Javascript 如何在redux中处理事件?(特别是异步的)

Javascript 如何在redux中处理事件?(特别是异步的),javascript,reactjs,redux,Javascript,Reactjs,Redux,大家好,我正在尝试处理来自异步redux操作的事件,但我不知道如何正确地处理它?在我的代码中,我的AddTopping()处理程序调用动作创建者,然后进程循环执行。我将所有内容都记录到了console.log——根据下面的代码,假设函数执行正常,console.log将按顺序打印出以下内容: success is 'pending' success is true 它首先打印初始状态,这是我不想要的。我必须给动作创建者打两次电话才能真正得到对象的“真”。我可以将setTimeout设置为50毫

大家好,我正在尝试处理来自异步redux操作的事件,但我不知道如何正确地处理它?在我的代码中,我的AddTopping()处理程序调用动作创建者,然后进程循环执行。我将所有内容都记录到了console.log——根据下面的代码,假设函数执行正常,console.log将按顺序打印出以下内容:

success is 'pending'
success is true
它首先打印初始状态,这是我不想要的。我必须给动作创建者打两次电话才能真正得到对象的“真”。我可以将setTimeout设置为50毫秒,它会工作,但我不想每次都设置为setTimeout。我将如何正确处理这件事

调用动作创建者的组件:

class Milk extends Component {
  componentWillMount() {
    this.AddTopping.bind(this);
  }
  AddTopping() {
    this.props.addChocolate({TableName: 'Dark', Item: {title: 'someCake'}});

    if (this.props.birthdayCake.send_success === true) {
      console.log('Congrats you have added chocolate to the cake');
    } else {
      console.log('You have failed to add chocolate to the cake.');
    }
  }
  render() {
    return (
      <div>
        <h1>Chocolate is great.</h1>
      </div>
    );
  }
}
export const addChocolate = (darkOrLight) => {
  return ( dispatch ) => {
    someAsyncFunction(darkOrLight, (err,data) => {
      if (err) {
        dispatch({ type: 'ADD_CHOCOLATE', success: false });
      } else {
        dispatch({ type: 'ADD_CHOCOLATE', success: true });
      }
    });
  }
}
const initialState = { send_success: 'pending' }
export function birthdayCake( state=initialState, action) {
  switch ( action.type ) {
    case ADD_CHOCOLATE:
      if (action.success === true) {
        return Object.assign({}, state, { send_success: true });
      } else {
        return Object.assign({}, state, { send_success: false });
      }
    default: 
      return state;
  }
}
现在将更新应用程序中状态的减速器:

class Milk extends Component {
  componentWillMount() {
    this.AddTopping.bind(this);
  }
  AddTopping() {
    this.props.addChocolate({TableName: 'Dark', Item: {title: 'someCake'}});

    if (this.props.birthdayCake.send_success === true) {
      console.log('Congrats you have added chocolate to the cake');
    } else {
      console.log('You have failed to add chocolate to the cake.');
    }
  }
  render() {
    return (
      <div>
        <h1>Chocolate is great.</h1>
      </div>
    );
  }
}
export const addChocolate = (darkOrLight) => {
  return ( dispatch ) => {
    someAsyncFunction(darkOrLight, (err,data) => {
      if (err) {
        dispatch({ type: 'ADD_CHOCOLATE', success: false });
      } else {
        dispatch({ type: 'ADD_CHOCOLATE', success: true });
      }
    });
  }
}
const initialState = { send_success: 'pending' }
export function birthdayCake( state=initialState, action) {
  switch ( action.type ) {
    case ADD_CHOCOLATE:
      if (action.success === true) {
        return Object.assign({}, state, { send_success: true });
      } else {
        return Object.assign({}, state, { send_success: false });
      }
    default: 
      return state;
  }
}

您不需要手动处理它,Redux会处理它。一旦状态更新,组件将重新呈现。你在哪里调用日志函数?这里只是一个猜测,但是你需要的这种异步行为可以用redux-thunk解决。你不需要手动处理它,redux会处理的。一旦状态更新,组件将重新呈现。你在哪里调用log函数?这里只是一个猜测,但是你需要的这种异步行为可以用redux thunk解决。