【问题标题】:React Native redux saga yield not working on first actionReact Native redux saga yield 不适用于第一个操作
【发布时间】:2018-12-23 19:06:43
【问题描述】:

我在我的 React Native 应用程序中使用 redux saga。除了一件事,这一切都有效。对于我第一次分派时的一个操作,它在下面的代码中停在“const newUserLogAction = yield take('CREATE_USER_LOG')”处。所以控制台日志 'saga callCreateUserLog take' 没有打印出来,什么也没有发生。但是,如果我再次发送相同的操作,它就会起作用。我还有其他动作,它们第一次都可以正常工作。

传奇文件:

function * createUserLog () {
  yield takeEvery('CREATE_USER_LOG', callCreateUserLog)
}

export function * callCreateUserLog () {
  try {
    console.log('saga callCreateUserLog start')
    const newUserLogAction = yield take('CREATE_USER_LOG')
    console.log('saga callCreateUserLog take')
    console.log('newUserLogAction' + FJSON.plain(newUserLogAction))
    const data = newUserLogAction.data
    const newUserLog = yield call(api.createUserLog, data)
    yield put({type: 'CREATE_USER_LOG_SUCCEEDED', newUserLog: newUserLog})
  } catch (error) {
    yield put({type: 'CREATE_USER_LOG_FAILED', error})
  }
}

动作文件:

export const createUserLog = (data) => {
  console.log('action create user log data = ' + FJSON.plain(data))
  return ({
    type: 'CREATE_USER_LOG',
    data}
  )
}

即使在第一次调度时,这里的数据也是正确打印的,因此有效负载是正确的。

react 本地函数进行调度:

clickContinue = () => {
    var event = {
      'userId': this.props.user.id,
      'eventDetails': {
        'event': 'Agreed to the ISA Declaration'
      }
    }
    this.props.dispatch(createUserLog(event))
  }

【问题讨论】:

    标签: react-native redux redux-saga


    【解决方案1】:

    你不需要使用take 效果。 Action 对象将由takeEvery 传递。

    export function * callCreateUserLog (newUserLogAction) {
      try {
        console.log('saga callCreateUserLog start')
        console.log('newUserLogAction' + FJSON.plain(newUserLogAction))
        const data = newUserLogAction.data
        const newUserLog = yield call(api.createUserLog, data)
        yield put({type: 'CREATE_USER_LOG_SUCCEEDED', newUserLog: newUserLog})
      } catch (error) {
        yield put({type: 'CREATE_USER_LOG_FAILED', error})
      }
    }
    

    【讨论】:

    • 非常感谢,就是这样!在我的其他动作中,他们是这样的,但由于某种原因不在这个特定的动作中,我没有发现它。
    • @moinhaque 很高兴我能帮上忙 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-26
    • 2018-07-20
    • 1970-01-01
    • 2018-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多