【问题标题】:Redux and Sagas: can't connect properlyRedux 和 Sagas:无法正确连接
【发布时间】:2020-06-17 08:13:34
【问题描述】:

我有一个使用 Saga 和 Redux Store 的项目,这是项目引导方法:

import { createStore, applyMiddleware } from 'redux';
import { Provider } from 'react-redux';
import { fork } from 'redux-saga/effects';

...

const bootstrap = async () => {
    const configuration = await config.env();
    init(configuration);

    const rootReducer = combineReducers({
        ...reducers,
    })

    function* rootSaga() {
        yield sagas.map(saga => fork(saga))
    }

    const sagaMiddleware = createSagaMiddleware();
    const store = createStore(rootReducer, applyMiddleware(sagaMiddleware));

    const App = (): Element<*> => (
        <Provider store={store}>
            <div>
                <WorstProjectInMyLife />
            </div>
        </Provider>
    );

    sagaMiddleware.run(rootSaga);

    ReactDOM.render(<App />, document.getElementById('app'));
};

bootstrap();

这就是我们向 props 发送状态的地方:

import { createSelector } from 'reselect';

const selectIsFetching = createSelector(
    parentSelector,
    (parentState: StateRecord = new StateRecord()) => parentState.isFetching
);

const mapStateToProps = (state: StateType) => ({
    isFetching: selectIsFetching(state)
});

const mapDispatchToProps = {
    ...actions
};

export default connect(mapStateToProps, mapDispatchToProps)(WorstProjectInMyLife);

但是商店总是空着,所以我担心这没有正确连接。各位大佬能看一下,是不是我漏了什么?我真的已经为此苦苦挣扎了几天并且变得精神恍惚,因为不知道为什么这不起作用。感谢您的帮助

【问题讨论】:

    标签: reactjs redux react-redux redux-saga


    【解决方案1】:

    我尝试设置一个与您类似的简单场景,它似乎有效。你能提供更多的上下文和代码吗?应用程序的整个状态一开始是未定义的? (记住您没有定义默认状态)父选择器是否正常工作?

    【讨论】:

    • 我尝试传递一个空对象或Immutable.Map({})作为默认状态,仍然没有工作。
    • 我已经编辑了答案...您能提供更多上下文吗?也许您可以尝试以某种方式在codesandbox.io 中复制它。
    猜你喜欢
    • 2018-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-01
    • 2016-08-07
    • 1970-01-01
    • 2021-05-08
    • 2017-12-07
    相关资源
    最近更新 更多