React Native redux 传奇产量不适用于第一个动作

React Native redux saga yield not working on first action

我在我的 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}
  )
}

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

响应本机函数进行调度:

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

您不需要使用 take 效果。动作对象将由 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})
  }
}