Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

effects异步执行后,再更新state怎么做? #1612

Closed
Mr-SPM opened this issue Apr 10, 2018 · 5 comments
Closed

effects异步执行后,再更新state怎么做? #1612

Mr-SPM opened this issue Apr 10, 2018 · 5 comments
Labels

Comments

@Mr-SPM
Copy link

Mr-SPM commented Apr 10, 2018

预期的功能是在effects异步获取数据后,弹窗显示获得的数据。设置组件state标志属性用于组件是否显示。如果把标志位属性放在model的state中在页面跳转的时候,该标志位仍会保留状态返回页面时弹窗就又出现了,这是我不想要的。

@sorrycc
Copy link
Member

sorrycc commented Apr 10, 2018

个人觉得弹窗 state 放在 react component 的 state 会比较合适。

@Mr-SPM
Copy link
Author

Mr-SPM commented Apr 10, 2018

如果加在react component 的 state中,那么要在effects回调后如何触发state更新呢?我的问题就是这个。走componentWillReceiveProps 这个的生命周期去改变吗?还是其他的方式?

@Mr-SPM
Copy link
Author

Mr-SPM commented Apr 10, 2018

我想到了个解决方案,不知道这样是否合适。
在dipatch effects action 的时候除了正常的payload ,我将更新state的方法作为参数同样传给了model 的effect,并在之后调用。代码如下:

//  route components.js
showDetail = (record) => {
    const { dispatch } = this.props;
    dispatch({
      type: effects.getDetail,
      payload: { id: record.id },
      callback: () => {
        this.setState({
          detailVisible: true,
        });
      },
    });
  };
// models
effects: {
*getDetail({ payload, callback }, { call, put }) {
      const response = yield call(getDetail, payload);
      if (response.success) {
        if (callback && typeof callback === 'function') {
          callback();
        }
        yield put({
          type: 'getDetailSuccess',
          payload: response,
        });
      }
    },
}

@WangSSGit
Copy link

不要用callback,每一个effects在dispatch后都会返回一个promise,在promise中处理弹窗就可以了

@Mr-SPM Mr-SPM closed this as completed Apr 13, 2018
@408709561
Copy link

不要用callback,每一个effects在dispatch后都会返回一个promise,在promise中处理弹窗就可以了

那怎么实现呢

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants