{"id":257428,"date":"2017-08-07T04:53:19","date_gmt":"2017-08-07T11:53:19","guid":{"rendered":"http:\/\/css-tricks.com\/?p=257428"},"modified":"2019-03-28T08:47:57","modified_gmt":"2019-03-28T15:47:57","slug":"react-router-4","status":"publish","type":"post","link":"https:\/\/css-tricks.com\/react-router-4\/","title":{"rendered":"All About React Router 4"},"content":{"rendered":"

I met Michael Jackson<\/a> for the first time at React Rally 2016, soon after writing an article on React Router 3<\/a>. Michael is one of the principal authors of React Router along with Ryan Florence<\/a>. It was exciting to meet someone who built a tool I liked so much, but I was shocked when he said. “Let me show you our ideas React Router 4, it’s way<\/em> different!” Truthfully, I didn’t understand the new direction and why it needed such big changes. Since the router is such a big part of an application’s architecture, this would potentially change some patterns I’ve grown to love. The idea of these changes gave me anxiety. Considering community cohesiveness and being that React Router plays a huge role in so many React applications, I didn’t know how the community would accept the changes.<\/p>\n

A few months later, React Router 4<\/a> was released, and I could tell just from the Twitter buzz there was mixed feelings on the drastic re-write. It reminded me of the push-back the first version of React Router had for its progressive concepts. In some ways, earlier versions of React Router resembled our traditional mental model of what an application router “should be” by placing all the routes rules in one place. However, the use of nested JSX routes wasn’t accepted by everyone. But just as JSX itself overcame its critics (at least most of them), many came around to believe that a nested JSX router was a pretty cool idea. <\/p>\n

So, I learned React Router 4. Admittedly, it was a struggle the first day. The struggle was not with the API, but more so the patterns and strategy for using it. My mental model for using React Router 3 wasn’t migrating well to v4. I would have to change how I thought about the relationship between the router and the layout components if I was going to be successful. Eventually, new patterns emerged that made sense to me and I became very happy with the router’s new direction. React Router 4 allowed me to do everything I could do with v3, and more. Also, at first, I was over-complicating the use of v4. Once I gained a new mental model for it, I realized that this new direction is amazing!<\/p>\n

My intentions for this article aren’t to rehash the already well-written documentation<\/a> for React Router 4. I will cover the most common API concepts, but the real focus is on patterns and strategies that I’ve found to be successful. <\/p>\n

React Router 5 is now available which is backwards compatible with React Router 4. It mostly has bug fixes and internal enhancements<\/a> to make it more compatible with React 16.<\/p>\n

Here are some JavaScript concepts you need to be familiar with for this article:<\/p>\n