I am using React + Redux and want to know how to properly update the state.
ReactDOM.render(
<Provider store={store}>
<Router history={browserHistory}>
<Route path="/questions" component={App}>
<Route path="subject">
<Route path="english" component={displayQuestions} />
<Route path="math" component={displayQuestions} />
<Route path="science" component={displayQuestions} />
<Route path="history" component={displayQuestions} />
</Route>
</Route>
...
</Router>
</Provider>
, document.querySelector('.app'));
You can click on the tabs on these routes (one example is shown here):
<div>
<Link to={'/questions/english'}>English</Link>
</div>
...
In my render method for displayQuestions, I check
if (!questions) {
Loading content
}
return (
{this.renderQuestions()}
)
So, when the user navigates to the page, in my componentDidMount () component, I use the action this.props.fetchQuestions method, and then it makes an async request for my backend, goes through the reducers, and then into the rendering method above. In the renderQuestions function, and in renderQuestions, I just grab the questions from this.props.questions.
, , componentDidMount . , english, DidMount, , url , componentDidMount .
, , userProfile, , . , , / , DidMount .
, URL? React + Redux? - .
EDIT:
:
componentDidUpdate(nextProps) {
if (this.props.route.path !== nextProps.route.path) {
let subject = this.props.location.pathname.split('/')[3];
this.props.fetchQuestions(subject);
}
return false;
}
, . .