I have an initial state of reduction:
{ loggedInUserId: null, comments: [] }
Here's what the React app looks like:
class App extends Component { componentWillMount() { this.props.getLoggedInUserId(); } render() { return ( <Switch> <Route exact path="/" component={HomePage} /> <Route path="/comments" component={Comments} /> </Switch> ); } }
In my application, I dispatch the getLoggedInUserId() action, which asynchronously populates loggedInUserId in state.
HomePage is a dumb component that displays some text. I launch the application (the route is now "/"), see the HomePage component, then I go to the "Comments" page, which has:
componentWillMount() { this.props.fetchComments(this.props.loggedInUserId); // Dispatch action to do API call to fetch user comments } render() { // Show this.props.comments nicely formatted }
Everything works, I see a list of comments in the Comments component.
But if I refresh the page on the /comments route, then by the time Comments start componentWillMount, loggedInUserId is not loaded yet, so it will call fetchComments(null) .
Right now, to fix this, I'm doing in the Comments component:
componentWillMount() { if (!this.props.loggedInUserId) return; this.props.fetchComments(this.props.loggedInUserId); } componentWillReceiveProps(nextProps) { if (!this.props.loggedInUserId && nextProps.loggedInUserId) { nextProps.fetchComments(nextProps.loggedInUserId); } }
which works well. But I do it in 10+ components, and it seems like a lot of work that can be factorized, but I have not found an elegant way to do this.
So, I ask you, how do you deal with this situation? Any idea is welcome:
- SPECIAL
- side effects
- other libraries