The mern documentation is pretty short when it comes to explaining this.
fetchComponentData ( , ) . , .
- , .
, .
posts Redux prop posts, .
function mapStateToProps(store) {
return {
posts: store.posts,
};
}
, API.
const initialState = { posts: [], selectedPost: null };
, , , Actions.fetchPosts().
export function fetchPosts() {
return (dispatch) => {
return fetch(`${baseURL}/api/getPosts`).
then((response) => response.json()).
then((response) => dispatch(addPosts(response.posts)));
};
}
, .
Caveat
React. , mern , fetchComponentData, , Redux .
fetchComponentData(store.dispatch, renderProps.components, renderProps.params)
, needs . "" promises.
const promises = needs.map(need => dispatch(need(params)));
return Promise.all(promises);
, Promise.all(promise) , Redux, .
, , ES6, .
ES6 , , , .
needs , promises fetchComponentData. , . , .
const fetchPosts = () => { return Actions.fetchPosts() };
const needs = [fetchPosts];
PostContainer.need = needs;