首页 > 解决方案 > 如何在 redux saga 中发送 id?

问题描述

刚开始了解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?

密码笔

标签: reactjsreduxreact-reduxredux-saga

解决方案


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

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


推荐阅读