Im doing React app with Redux and React-Router
For example i have 6 million profile pages
/id1
/id2
...
/id6000000
and they all should be draw by ProfilePage component. so i just doing something like:
class ProfilePage extends Component {
componentDidMount() {
httpApi.getProfileData(this.props.match.params.id)
.then(response => {
this.setState({
page: response
})
})
}
...
}
Its works great.
But next i want to use Redux for the same things (i will call dispatch directly for easy reading).
Also we need to understand that we not need page data in global store, except as for use connected props in underlying components(we can easly replace it by createContext). We just use Redux to move logic and request in outer space.
class ProfilePage extends Component {
static propTypes = {
page: propTypes.object // from connect
}
componentDidMount() {
dispatch({type: 'PROFILEPAGE_LOADING_START', id: this.props.match.params.id});
}
...
}
which after middle-ware call a reducer and set store:
PROFILEPAGE_LOADING_SUCCESS:
return {
profilePage: action.page
}
So i think now everything works fine, except one thing.
For example we stay at profile page with id1 and going to id3, our react-router unmount profilePage[id1] and mount profilePage[id3], and its render with old[id1] data before new data is loaded, because we not remove old data from store. Hmm.
I understand we can implement a page keys or loading state or clear state at componentDidMount (but it still render with old data before mounting) something else, but it is a bit ugly and have side effects.
Question: What the best practice?
And second, if i want to implement prefetch data loading at link click before history push. Its very easy to implement without Redux, just use history push state and use this state in componentDidMount.
But if I use Redux then its ABSOLUTELY TERRIBLE thing. For first i need to resolve first question by any ugly way, and second i need to join together somehow data change in store and render by react router. Otherwise need to implement shouldComponentUpdate logic, which is ugly too and not trigger in some cases.
I tried a lot of solutions and they work but all ugly as hell.
Question 2: What the solution?
idchanges, the component gets updated, you can try to usecomponentDidUpdate()simpler Api to shouldComponentUpdate. then compare the previous props with the new props, and only re-render the component when it's a new prop