【发布时间】:2018-04-25 07:54:26
【问题描述】:
我想在react-native中从另一个reducer更新一个reducer的状态。我的代码是这样的。
这是动作。
export const doLike = payload => {
// Recieving response from the server
let updatedPost = {
Id: 1,
userId: 1,
postId: payload.post._id,
__v: 1,
count: payload.type === 1 ? 10 : 1
};
return {
type: SM_ACTION_LIKE,
post: updatedPost
};
};
这是接受动作的 smReducer。
const initialState = {
post: {}
};
const smReducer = (state = initialState, action) => {
switch (action.type) {
case SM_ACTION_LIKE:
return {
...state,
post: action.post
};
break;
default:
return state;
}
return state;
};
export default smReducer;
现在我想从这里更改 mainFeedReducer 的帖子数组。我的 mainFeedReducer 就是这个。我想从 smReducer 访问 mainFeedReducer 的 posts 数组。
const initialState = Immutable({
posts: [],
featuredWorkouts: []
});
const mainfeedReducer = (state = initialState, action) => {
switch (action.type) {
case FETCH_MAIN_FEED:
return {
...state,
posts: action.mainFeedData.posts,
featuredWorkouts: action.mainFeedData.featuredWorkouts
};
break;
default:
return state;
}
};
我怎样才能做到这一点?
【问题讨论】:
标签: react-native redux