如何将数据从 API 响应存储到状态?

时间:2021-01-11 13:27:50

标签: reactjs redux react-redux

我想存储对 state 的 API 调用的响应。每当我刷新页面时,它都会丢失其数据/值。我为它写了一个传奇

export function* maintainCurrentUserState(action) {
  if (!this.action) {
    yield put(getCurrentUserSaga());
  }
}

但这没有任何意义。基本上,我的方法是在状态中找不到数据时调用 getCurrentUserSaga() 方法。

这是相同的组件

  componentDidMount() {
    this.props.getCurrentUser();
    bsCustomFileInput.init();
  }
...
const mapStateToProps = state => {
  return {
    loading: state.getCurrentUser.loading,
    error_messages: state.getCurrentUser.errors,
    response: state.getCurrentUser.successMessage
  };
};

const mapDispatchToProps = dispatch => {
  return {
    getCurrentUser: () => dispatch(actions.getCurrentUser())
  };
};

export default compose(
  withTranslation(),
  connect(mapStateToProps, mapDispatchToProps)
)(ProfileSettings);

0 个答案:

没有答案