【问题标题】:redux-saga takeEvery only called with setTimeoutredux-saga takeEvery 仅使用 setTimeout 调用
【发布时间】:2016-06-16 21:08:32
【问题描述】:

我有一个调度以下操作的容器:

const mapDispatchToProps = (dispatch, ownProps) => { 
    return { 
        getPageTree: (siteId) => { 
            dispatch(getPageTree(siteId)); 
        }
    }
} 

const explorer = connect(
    mapStateToProps, 
    mapDispatchToProps
)(Explorer); 

在我的组件(通过容器发送道具)中,我添加了:

componentWillMount(){ 
    setTimeout(() => this.props.getPageTree()); 
}

传奇:

function* fetchPageTree(action){ 
    try{ 
        const data = yield call(Api.fetchPageTree, action.payload.url); 
        yield put({type: 'FETCH_SUCCEEDED', data}); 
    }catch(error){ 
        yield put({type: 'FETCH_FAILED', error})
    } 
}

export function* watchFetchData(){ 
    console.log('watch'); 
    yield* takeEvery('GET_PAGETREE', fetchPageTree); 
    console.log('finish watch'); 
}

当在组件中应用setTimeout 时,saga watcher 使用takeEvery 正确调用fetchPageTree,但不执行fetchPageTree
也不会抛出任何错误。

知道原因吗?

编辑:
index.es6 文件包含以下初始化逻辑,但总体标准:

const sagaMiddleware = createSagaMiddleware(); 
const store = createStore(
    appReducers, //Combined reducers 
    applyMiddleware(sagaMiddleware)
); 

render(
    <Provider store={store}>
        <App />
        </Provider>, 
    document.getElementById('zp-app')
)
sagaMiddleware.run(rootSaga); 

【问题讨论】:

  • getPageTree(siteId) 返回什么?应用 setTimeout 是什么意思?
  • @baisang 是一个action creator,返回一个action对象来获取pageTree。我认为这个问题与组件方法在传奇之前被调用的事实有关。 setTimeout 让其他进程在函数运行之前完成。虽然目前不知道如何解决这个问题。
  • 你确定 setTimeout() 和 getPageTree(siteId) 的动作输出格式相同吗?
  • @html_programmer 你有没有解决过这个副作用/异步问题?我在调用操作时遇到了同样的问题,但永远不会触发 saga take latest 但如果我在 mount 方法中使用设置的超时,则可以工作。

标签: javascript redux redux-saga


【解决方案1】:

您何时调用render 函数? 如果在sagaMiddleware.run(rootSaga); 之后调用渲染函数,它应该可以在this.props.getPageTree 的延迟调用中工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-21
    • 2020-01-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多