【问题标题】:How do I dispatch the id in redux saga?如何在 redux saga 中发送 id?
【发布时间】:2018-10-06 10:14:30
【问题描述】:

刚刚了解redux saga,如何在dispatch中传入我的数据? 目前我的根传奇是这样的:

function* getData(id){
  const data = yield fetch('https://jsonplaceholder.typicode.com/todos/'+id)
  .then(response=>response.json())

  yield put({type:"RECEIVED_DATA",json:data})
}

export default function* rootSaga(){
  yield takeEvery("GET_DATA",getData)
}

如何在 getData 生成器中发送 id?

codepen

【问题讨论】:

    标签: reactjs redux react-redux redux-saga


    【解决方案1】:

    您可以通过action 对象获取id。像这样:

    function* getData(action){  // fixed
      const id = action.id // we get the `id` from the `action` object
    
      const data = yield fetch('https://jsonplaceholder.typicode.com/todos/'+id)
      .then(response=>response.json())
    
      yield put({type:"RECEIVED_DATA",json:data})
    }
    
    export default function* rootSaga(){
      yield takeEvery("GET_DATA",getData)
    }
    

    这是演示:https://codesandbox.io/s/ojlyzyq5oy

    【讨论】:

      猜你喜欢
      • 2019-02-26
      • 2017-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多