[英]React Redux: how do you access response of post in component?
I'm working on a project based on this: https://github.com/bradtraversy/lead_manager_react_django我正在开发一个基于此的项目: https://github.com/bradtraversy/lead_manager_react_django
I need to access the pk of a newly created object.我需要访问一个新创建的 object 的 pk。 I can console.log the values I need in the action but cannot figure out how to use it in the component.
我可以控制台记录我在操作中需要的值,但无法弄清楚如何在组件中使用它。 Redux tools shows the new values as well in the diff.
Redux 工具也在差异中显示了新值。
What am I missing?我错过了什么?
Action:行动:
export const addPlan = (plan) => (dispatch, getState) => {
axios.post("/api/plans/", plan, tokenConfig(getState))
.then(res => {
dispatch(createMessage({ createPlan: 'Plan created.'}));
dispatch({
type: ADD_PLAN,
payload: res.data
});
}).catch(err => dispatch(returnErrors(err.response.data, err.response.status)));
Reducer:减速器:
case ADD_PLAN:
return {
...state,
plans: [...state.plans, action.payload]
};
mapStateToProps in component:组件中的 mapStateToProps:
const mapStateToProps = state => ({
plans: state.plans.plans,
production: state.plans.production
});
You need to use mapStateToProps
with connect
inside the component.您需要在组件内部使用
mapStateToProps
和connect
。
function mapStateToProps(state) {
const { plans } = state
return { todoList }
}
export default connect(mapStateToProps)(YourComponent)
You can read more about connecting your component with redux store here您可以 在此处阅读有关将组件与 redux 商店连接的更多信息
Turns out, everything was setup correctly but I just didn't know where to get access to the new props.事实证明,一切都设置正确,但我只是不知道从哪里获得新道具。 I used componentWillReceiveUpdate and a little logic to prevent default and it works like a charm.
我使用了 componentWillReceiveUpdate 和一些逻辑来防止默认,它就像一个魅力。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.