亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么在使用 thunk 時會以錯誤的順序調用我的動作創建者?

為什么在使用 thunk 時會以錯誤的順序調用我的動作創建者?

神不在的星期二 2023-10-14 11:16:18
我正在嘗試 redux-thunk 和動作創建者,并注意到一些我不理解的奇怪行為。當我調用動作創建器函數時,它們不會按照我希望的順序調用。這是我的 App.js 組件class App extends Component {  handleSave = () => {    this.props.postData({      name:this.props.activity,      type_name: this.props.type    })    this.props.fetchList()    this.props.fetchData()  }  handleClick = () => {    this.props.fetchData()  }  componentDidMount() {    this.props.fetchData()    this.props.fetchList()  }  render() {    return (      <div className="App">        <Router>          <Nav />          <Route exact path='/' render={props => <Home {...props} clickProp={this.handleClick} saveProp={this.handleSave}/>} />          <Route exact path='/activities' render={props => <ListContainer {...props} numItems={this.props.list.length} listProp={this.props.list}/>} />        </Router>      </div>    );  }}const mapStateToProps = state => {  return {    activity: state.activity,    type: state.type,    list: state.list  }}const actions = {fetchData, fetchList, postData}export default connect(mapStateToProps, actions)(App);當我單擊 Home 子組件中的按鈕時,將調用 handleSave 函數,然后該函數應該將一個項目發布到我的列表中,然后獲取更新的列表,以便它可以顯示在我的列表中。當我這樣做時, this.props.fetchList() 首先被調用,即使它是第二個被調用的函數。我在我的減速器中放置了一個 console.log(action) ,這就是打印的內容。{type: "FETCH_LIST", payload: Array(86)}{type: "POST_DATA", payload: {…}}{type: "FETCH_DATA", payload: {…}}我可以讓 FETCH_LIST 在 POST_DATA 之后發生的唯一方法是如果我像這樣第二次調用 fetch_data()  handleSave = () => {    // something weird going on here    this.props.fetchList()    this.props.postData({      name:this.props.activity,      type_name: this.props.type    })    this.props.fetchList()    this.props.fetchData()  }如果可能的話,我真的希望能夠讓我的代碼正常工作,而不必兩次調用同一個函數。最后,這就是我的動作創建者的樣子。export default function fetchData() {  return (dispatch) => {    const url = 'http://www.boredapi.com/api/activity/'    fetch(url)      .then(res => res.json())      .then(activity => { dispatch({type: "FETCH_DATA", payload: activity})})  }}我唯一的猜測是,發生這種情況是因為這些操作是異步的。所以我也嘗試改變這些函數的調用順序,無論什么 FETCH_LIST 總是發生在 POST_DATA 之前。
查看完整描述

1 回答

?
BIG陽

TA貢獻1859條經驗 獲得超6個贊

您可以將這些操作創建者轉換為async版本,然后您可以等待它們,以便它們按順序執行。


https://medium.com/@gaurav5430/async-await-with-redux-thunk-fff59d7be093


例如在你的fetchData


function fetchData() {

  return async (dispatch) => {

    const url = 'http://www.boredapi.com/api/activity/'


    try{

      const res = await fetch(url)

      const activity = await res.json();

      dispatch({type: "FETCH_DATA", payload: activity})

    }

    catch (error) {

      console.log(error);

    }

}

一旦它們出現,async您就可以在您的中等待它們handleSave(一旦您將其轉換為async),這將確保它們按順序被調用。


handleSave = async () => {

    await this.props.postData({

      name:this.props.activity,

      type_name: this.props.type

    })

    await this.props.fetchList()

    await this.props.fetchData()

  }


查看完整回答
反對 回復 2023-10-14
  • 1 回答
  • 0 關注
  • 122 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號