Javascript 在另一个减速器中更改状态
我开始研究反应。这是我的问题。我有一些减速机Javascript 在另一个减速器中更改状态,javascript,reactjs,state,Javascript,Reactjs,State,我开始研究反应。这是我的问题。我有一些减速机 let reducers = combineReducers({ user: userReducer, index_page: indexReducer, notifications: notificationsReducer }); Notifications Reducer有自己的渲染通知状态,在indexReducer中有。一个axios请求,在响应后,应在通知还原器中向用户发出更改状态通知 我不太明白怎么做 这是我的
let reducers = combineReducers({
user: userReducer,
index_page: indexReducer,
notifications: notificationsReducer
});
Notifications Reducer有自己的渲染通知状态,在indexReducer中有。一个axios请求,在响应后,应在通知还原器中向用户发出更改状态通知
我不太明白怎么做
这是我的代码:
通知减速器
let initialState = [
{id: 3, text: 'test_msg', state: 'error'}
];
export const createNotificationActionCreator = (msg_text, msg_state) => {
return {
type: 'SHOW_NOTIFY',
msg_text: msg_text,
msg_state: msg_state
}
}
const notificationsReducer = (state = initialState, action) => {
switch (action.type) {
case SHOW_NOTIFY:
let msg = {
text: action.msg_text,
msg_state: action.msg_state
};
state.push(msg);
break;
}
return state;
}
const indexReducer = (state = initialState, action) => {
switch (action.type) {
case CREATE_NEW_BET:
let bet_data = new Object();
bet_data.bet = state.betAmount;
bet_data.color = action.color;
axios.get('http://localhost/createbet', {
params: {
bet_data
}
}).then(function (response) {
// CHANGE STATE IN notificationsReducer
});
break;
}
return state;
}
indexReducer
let initialState = [
{id: 3, text: 'test_msg', state: 'error'}
];
export const createNotificationActionCreator = (msg_text, msg_state) => {
return {
type: 'SHOW_NOTIFY',
msg_text: msg_text,
msg_state: msg_state
}
}
const notificationsReducer = (state = initialState, action) => {
switch (action.type) {
case SHOW_NOTIFY:
let msg = {
text: action.msg_text,
msg_state: action.msg_state
};
state.push(msg);
break;
}
return state;
}
const indexReducer = (state = initialState, action) => {
switch (action.type) {
case CREATE_NEW_BET:
let bet_data = new Object();
bet_data.bet = state.betAmount;
bet_data.color = action.color;
axios.get('http://localhost/createbet', {
params: {
bet_data
}
}).then(function (response) {
// CHANGE STATE IN notificationsReducer
});
break;
}
return state;
}
要更新另一个reducer中的状态,我建议在调度
CREATE\u NEW\u BET
之后立即调度SHOW\u NOTIFY
操作。这可以通过使用
另请阅读关于更新由另一个reducer管理的状态的建议的堆栈溢出回答:
使用redux thunk设置,thunk的外观如下:
const createBetAndNotify = () => (dispatch) => {
return dispatch({ type: "CREATE_NEW_BET" }).then(() => {
dispatch({ type: "SHOW_NOTIFY" })
})
}
然后在React组件中,您将发送上述thunk:
dispatch(createBetAndNotify());