Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
141 views
in Technique[技术] by (71.8m points)

reactjs - How to store data from API response to state?

I want to store the response from an API call to the state. Whenever I refresh the page it losses its data/values. I wrote a saga for it

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

but this does not make any sense. Basically, my approach is to recall the getCurrentUserSaga() method when it the data is not found in the state.

This is the component for the same

  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);


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...