Answer the question
In order to leave comments, you need to log in
Where to load initial data for React + Redux state?
I get the initial state from the API in the container component (smart component):
function App(props){
useEffect(() => {
if(!props.isLoaded){
props.getInitialState();
}
});
return <MyBlock data={props.data} />;
}
const mapStateToProps = store => {
return { ...store};
};
const mapDispatchToProps = dispatch => {
return {
getInitialState: () => dispatch(getInitialState())
};
};
export default connect(mapStateToProps, mapDispatchToProps)(App);
function getInitialState() {
return dispatch => {
dispatch({
type: 'GET_INITIAL_DATA'
});
fetch('url')
.then(res => res.json())
.then(data => {
dispatch => {
type: 'GET_INITIAL_DATA_SUCCESS',
payload: data
}
});
};
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question