简体   繁体   English

React Redux:你如何访问组件中帖子的响应?

[英]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.您需要在组件内部使用mapStateToPropsconnect

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.

相关问题 在功能组件中,如何使用 react-redux 连接从 redux 商店访问道具? - In a Functional Component, how do you access props from the redux store using react-redux connect? 如何通过将Redux连接的React组件导出为PureComponent来测试它? - How do you test a redux connected react component by exporting it as a PureComponent? 我如何从另一个 React 组件访问 redux 状态? - How do i access redux state from another react component? 如何在React包装器组件中访问子项的DOMNodes / refs? - How do you access the DOMNodes/refs of children in a React wrapper component? React Native:通过 ref 访问组件时,如何访问该组件的方法? - React Native: When accessing a component via ref, how do you access methods of that component? 如何从React组件访问Redux存储以进行身份​​验证 - how to access redux store from react component for authentication 如何访问组件外部的react-redux存储 - How to access react-redux store outside of component React:如何从Redux / Flux操作访问组件引用? - React: How to access component refs from Redux/Flux actions? 您如何通过Express访问Axios响应? - How do you access Axios response with Express? 如何访问 react-redux 存储在 redux-toolkit 中的反应组件之外? - How to access react-redux store outside react component in redux-toolkit?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM