简体   繁体   English

Redux-Saga行为模式

[英]Redux-Saga behavior pattern

Such a saga works perfectly well: 这样的传奇效果非常好:

function* getPosition() {
  yield navigator.geolocation.getCurrentPosition(function(pos) {
    console.log(`I am getPosition: ${pos.coords.latitude}, ${pos.coords.longitude}`);
  });
}

But I need that coordinates in Redux state tree. 但是我需要Redux状态树中的坐标。 So, I tried a few patterns and none of them works. 因此,我尝试了几种模式,但没有一种起作用。 1) There is no way to get variable out of getCurrentPosition scope 1)没有办法使变量超出getCurrentPosition范围

function* getPosition() {
  let position = {};
  yield navigator.geolocation.getCurrentPosition(function(pos) {
    position = pos;
  });
  // either
  console.log(`I am getPosition: ${position.coords.latitude}, ${position.coords.longitude}`);
  // or
  yield console.log(`I am getPosition: ${position.coords.latitude}, ${position.coords.longitude}`);
  // Any of two is undefined
}

2) There is no way to return and assign value: 2)无法返回和分配值:

function* getPosition() {
  const position = yield navigator.geolocation.getCurrentPosition(function(pos) {
    return pos;
  });
  yield console.log(`I am getPosition: ${position.coords.latitude}, ${position.coords.longitude}`);
}

3) Method put has no effect: 3)放置方法无效:

function* getPosition() {
  yield navigator.geolocation.getCurrentPosition(function(pos) {
    // Pos fetched
    console.log(`I am getPosition: ${pos.coords.latitude}, ${pos.coords.longitude}`);
    // Nothing happens. State is empty object.
    put({
      type: LOCATION_SET_POSITION,
      pos
    });
  });
}

locationReducer is inside of rootReducer as the other working reducers are: locationReducer在rootReducer内部,因为其他工作的reducer是:

locationReducer.js
export function locationReducer(state = {}, action) {
  switch (action.type) {
    case LOCATION_SET_POSITION:
      return action.pos
    default:
      return state;
  }
}

And I do not have an actionCreater. 而且我没有actionCreater。 As far as I understand, put method both dispatches an action and sets actionCreator. 据我了解, put方法既可以调度动作,也可以设置actionCreator。 How to put coordinates to the state tree? 如何将坐标放入状态树?

Your problem is that geolocation.getCurrentPosition is asynchronous, but is in the success/error callback style, whereas you need it to be a promise to be fed to redux-saga 您的问题是geolocation.getCurrentPosition是异步的,但具有成功/错误回调样式,而您需要将其作为兑现给redux-saga的保证

function* getPositionSaga() {
    const getCurrentPosition = () => new Promise(
      (resolve, reject) => navigator.geolocation.getCurrentPosition(resolve, reject)
    )
    const pos = yield call(getCurrentPosition)
    yield put({type: LOCATION_SET_POSITION, pos})
}

Here we wrap getCurrentPosition into a function that returns a Promise<Position> 在这里,我们将getCurrentPosition包装到一个返回Promise<Position>的函数中

call is a redux-saga effect that if the function it is given returns a promise, it will yield only when that promise is fulfilled, and will return the fulfilled value into your saga for further use. call是一个redux-saga效果,如果给定的函数返回一个promise,则仅在该promise满足时才会产生,并将实现的值返回给您的saga以供进一步使用。

put is an effect that will end up dispatching the given action object via redux put是一种效果,最终将通过redux调度给定的action对象

Any redux-saga effects must be yielded from the generator rather than straight up called, as they return just a simple object of instructions for the redux-saga middleware executor (rather than actually performing the side effect immediately). 任何redux-saga效果都必须从生成器中产生,而不是直接调用,因为它们仅为redux-saga中间件执行程序返回一个简单的指令对象(而不是立即实际执行副作用)。 The executor can only access and control these when yielded from the generator, so using them in callbacks like your example 3 will not work like you expect it to 执行程序只能在生成器产生时访问和控制它们,因此在示例3之类的回调中使用它们将无法正常工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM