Skip to main content

Posts

Showing posts from May, 2017

JavaScript: Porting from React Router v2 to v4 (with tips for Enzyme and MobX)

I recently ported a large app from React Router v2 to v4. It was hard. I jotted down some notes in case you have to do the same thing.First of all, start with the video, and look around in the Quick Start. The video will explain why v4 is better, and the Quick Start has a bunch of examples that you may find useful. I'm not going to duplicate all that knowledge. I'm just going to touch on some details.Here's a little utility to configure the default routing configuration:import $ from 'jquery'; /** * This is the default configuration for React Router v4. * * Usage: * *     <Router { ...getDefaultReactRouterConfig() }> */ export default function getDefaultReactRouterConfig() { const args = {}, href = $('base').attr('href'); // Make sure to strip the trailing slash from the base href; otherwise, it causes     // a double slash issue on route changes to the root route. It is also not     // possible to not have the trailing slash at the end of the base href,   …