简体   繁体   English

Redux/React 中的 Action dispatch 不更新 state

[英]Action dispatch not updating state in Redux/React

I'm dispatching an action from a component, everything looks like it's linked properly but when I console.log the state in my component props it has not updated.我正在从一个组件调度一个动作,一切看起来都正确链接但是当我在我的组件道具中控制台记录 state 它没有更新。

I have tried reformatting my code and looked at multiple examples, and it looks like it's supposed to run?我已经尝试重新格式化我的代码并查看了多个示例,它看起来应该可以运行? When I log from the reducer it's receiving the action it's just not updating the state.当我从 reducer 登录时,它正在接收操作,只是没有更新 state。

    const mapStateToProps = state => ({
         todos: state
    });

    onSubmit(e) {
        e.preventDefault();
        let payload = this.state.content
        this.props.dispatch(post_todo(payload));
        console.log(this.props.todos)
        this.setState({
            content: ""
        })
    }
export default (
  state = [],
  action
) => {
  switch (action.type) {
    case POST_TODO:
      console.log("got it")
      console.log(action.payload)
      console.log(state)
      return [
        ...state,
        action.payload
      ];
    default:
      return state;
  }
};
export function post_todo (payload){
    return {
        type: POST_TODO,
        payload
    };
}

It should update the props.todos to the proper state but its showing an empty array everytime.它应该将 props.todos 更新为正确的 state 但它每次都显示一个空数组。

onSubmit(e) {
    e.preventDefault();
    let payload = this.state.content
    this.props.dispatch(post_todo(payload)); <=== this line
    console.log(this.props.todos)
    this.setState({
        content: ""
    })
}

when you dispatch you action at the line I'm point at, it will go and exec your actions, and then you could receive the newly updated props in an event like componentWillReceiveProps ...当你在我指向的那条线上调度你的动作时,它会去执行你的动作,然后你可以在像componentWillReceiveProps这样的事件中接收新更新的道具......

receiving new props to your component will cause your component to re render为您的组件接收新props将导致您的组件重新render

So, console logging your props immediately after executing your action, will never give you the new state ... wait for it in componentWillReceiveProps or render methods因此,控制台执行您的操作后立即记录您的道具,永远不会为您提供new state ......在componentWillReceivePropsrender方法中等待它

here's an example of how to get your new props (todos) in your case:这是如何在您的案例中获取新道具(todos)的示例:

componentWillReceiveProps(nextProps) {
  const { todos } = nextProps;

  console.log('I recieved new todos', todos);
}

also, if your render method render any component that display this todos field that you grap from this.props.todos ... also will be updated automatically...此外,如果您的render方法渲染任何显示您从this.props.todos todos字段的this.props.todos ......也将自动更新......

You can access the store from the component immediately as It'll contain the new value which hasn't made it yet to the component您可以立即从组件访问商店,因为它将包含尚未进入组件的新值

import store  from "../../redux/configureStore"
const requiredState = store.getState()[path of the required value];

I'm not sure if this is an anti-pattern though.我不确定这是否是反模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM