首页 > 解决方案 > Redux 和 Sagas:无法正确连接

问题描述

我有一个使用 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);

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

标签: reactjsreduxreact-reduxredux-saga

解决方案


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


推荐阅读