比如一个博客,首次进入页面,我从后端同时获得了user,article,comment,我是一个一个传递给他们吗?
就像这样
const article = (article) => ({type: 'INIT_ARTICLE', article});
const user = (user) => ({type: 'INIT_USER', user});
const comment = (user) => ({type: 'INIT_COMMENT', comment});
获取后触发三个dispatch吗?
目前我的做法
const blogReducer = (state = {}, action) => {
switch (action.type) {
case RECEIVE_DATA:
return action.data;
default:
return {
comment: commentHelper(state.comment, action),
article: articleHelper(state.article, action),
user: userHelper(state.user, action),
}
}
}
const commentHelper = (state = [], action) => {
switch (action.type) {
case 'ADD_COMMENT':
return [...state, action.comment];
default:
return state;
}
}
获取数据时触发RECEIVE_DATA,更改comment时,触发ADD_COMMENT就行。
不知是否合理。
MMMHUHU
相关分类