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

Categories

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

javascript - React executing action call one after another is not working

export function updateSchools(subjectId, schoolId, data) {
const { year } = data
  return {
    type: actionTypes.UPDATE_SCHOOL,
    meta: actionTypes.UPDATE_SCHOOL,
    payload: http.put(
      `api/school/${schoolId}`,
      data
    ).then(data => {
      return _getSchools(subjectId, year)
    })
  }
}


const _getSchools = (subjectId, year) => {
  return http.get(`api/school/${subjectId}/subject/${year}`).then(data => {
    //some logic
    
    return data
  })
}

Upon calling the action updateSchools function, the updated value is not returning in _getSchools response. When the page is reloaded the updated value is returned and even debugging at updateSchools function, in the database the value is updated. Can anyone please tell me where I'm wrong? Promise and react-redux are used in the application.

question from:https://stackoverflow.com/questions/65881013/react-executing-action-call-one-after-another-is-not-working

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

1 Reply

0 votes
by (71.8m points)

Your payload that you're sending in the action is a Promise. Please check the Redux documentation,

An action is a plain JavaScript object

This means that you shouldn't send any promises or other functions in your action. Promises should start and end elsewhere outside of redux actions and reducers.

The common way fetching data is through the Redux middleware. To name one middleware - redux-thunk. You can definitely avoid using middlewares and write all by yourself, it's up to you. But one thing you should change in your app -- is stop sending a promise in action's payload.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...