我有一个使用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();
这是我们将状态分配给道具的地方:
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);
但是商店总是空着,所以我担心连接不正确。你们能看看吗,也许我错过了什么?我已经为这几天忙得不可开交了,并且变得不安,因为不知道为什么这行不通。感谢您的帮助
我认为createStore()
的第二个参数应为默认状态:
const store = createStore(
rootReducer,
rootDefaultState,
applyMiddleware(sagaMiddleware)
)
检查https://redux.js.org/api/createstore和https://redux.js.org/api/applymiddleware。尝试一下,告诉我ti是否有效。