Once upon a time, react-router transferred params and query params from the url via props into components. It doesn’t happen this way any more. So, let’s say you have a need in your component like overriding
shouldComponentUpdate in your component which only has access to
this.state. How can you use the url data to affect the component re-render or not?
Why control rendering with the url?
There are two instances that come to mind when considering the scenarios that it might be needful to control the update of your component based on the url:
Avoid re-render: Of course, if the component we’re talking about is a page-level component, you’ll most likely always be wanting to re-render as the url changes. But, if you’re a little widget component that cares about the url, you might not need to always re-render on url change.
Trigger re-render when it might not otherwise happen: For whatever reason, you may have already needed to implement
shouldComponentUpdatein your component. This function returns true or false to flag a “should” or “should not” re-render. You’ll need to access url state and likely previous url state in this method to make re-renders happen that otherwise wouldn’t if you had no logic in this function for it.
Of course, beyond these re-render scenarios, there are many other situations in which knowing previous and current states in the url could come in handy.
React-Router in shouldComponentUpdate
shouldComponentUpdate determines if component should re-render. It’s called whenever
this.state changes. It has access to incoming props and state via the function parameters.
React-router gives your component access to params and query params by requiring you to mix in the
State param from react-router. Then you have access to new functions,
this.getQuery(). If you only need to know current url state, this is enough. If you want to compare current url state to previous url state, you’ll have to save the parts that you care about. We’ll do that in
Let’s say that we want to trigger a redraw on changes to a
range query parameter. The component might, in part, look like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
When the url params change,
componentWillReceiveProps is called. But this is not called on the first render, thus we implement
componentWillMount. Both these functions save the
range from the url to the component state. Now, as
shouldComponentUpdate is called on future
this.state changes, current and next
range values can be compared and used in “should re-render” calculation.
What other ways have you found to accomplish this tracking of previous state in the url?