【发布时间】:2020-05-31 13:58:39
【问题描述】:
我不明白如何从减速器存储中获取更新的值。例如,我有一个 React 组件。在这个组件中,在一些操作之后,例如,在一个按钮上单击几下之后,我从我的 reducer 操作脚本中调用该操作,例如 this.props.PostSomethingToServer()。然后该操作将一些数据发送到节点快递服务器。服务器对数据进行一些更改,然后向客户端存储减速器发送响应。如何从 reducer 存储中获取更新的数据?我需要使用更新的值调用此 React 组件中的另一个函数。
顺便说一句,我在 React 组件中使用了mapStateToProps 和export default connect()。据我所知,mapStateToProps 和export default connect() 有助于在render() 之前从存储中获取数据。但是我仍然不明白如何在某些操作后从商店获取更新的数据。
几个代码:
反应组件:
ChangeArrayData(){
let something = [];
// filling this array and modifying values
//then I call the action
this.props.postSomethingToServer(something);//something will be changed by server logic and will be returned with updated data by a response.
//then I wanna export the data to .excel, for example
this.ExportToExcel(); // here I have to get updated data from the reducer, but I don't have any information about changes in the reducer.
}
减速器动作:
export const postSomethingToServer= rankedElements => dispatch => {
axios
.post("/api/postData", elements)
.then(response => {
dispatch({
type: POST_SOMETHING_SUCCESSFUL,
status : "success",
payload: response.data
});
//... etc.
减速机:
const initialState = {
something: {},
status: "",
error : ""
};
export default function(state = initialState, action) {
switch (action.type) {
case POST_SOMETHING:
return {
...state,
status: action.status,
}
case POST_SOMETHING_SUCCESSFUL:
return {
...state,
status: action.status,
something: action.payload
}
case GET_ERRORS:
return {
...state,
status: action.status,
error: action.error
}
default:
return state;
}
}
【问题讨论】:
-
你能在问题中添加reducer代码吗?
-
@AjinKabeer 我刚刚更新了代码
标签: reactjs react-redux reducers