不可能组合减速器
铅是什么?
当我执行 pull origin main 时,除了我的分支之外,我还使用减速器恢复了另一个分支。所以,我和减速机发生了冲突。我在reducers文件夹中创建了一个index.js以便将它们结合起来。在 store redux 中,我调用了 index.js 减速器,但状态不会在 redux devtools 中读取。
代码
存储
import { createStore, applyMiddleware, compose } from 'redux';
import reducer from 'src/reducers/genres';
import regionsMiddleware from '../middlewares/regionsMiddleware';
import genresMiddleware from '../middlewares/genresMiddleware';
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
const enhancers = composeEnhancers(
applyMiddleware(
genresMiddleware,
regionsMiddleware,
),
);
const store = createStore(
// reducer
reducer,
// enhancer
enhancers,
);
export default store;
REDUCER(jindex)
import { combineReducers } from 'redux';
import genresReducer from './genres';
import regionsReducer from './regions';
const rootReducer = combineReducers({
genres: genresReducer,
regions: regionsReducer,
});
export default rootReducer;
GENRES(reducer)
import { SAVE_GENRES } from '../actions/genres';
const initialState = {
genres: [],
};
const reducer = (state = initialState, action = {}) => {
console.log(`le reducer a reçu une action ${action.type}`);
switch (action.type) {
case SAVE_GENRES:
/*
- on crée un nouvel objet : {}
- on déverse dedans toutes les informations du state actuel : ...state
- on écrase une valeur qui provenait du state actuel
*/
return {
...state, // list: state.list
genres: action.genres,
};
default:
return state;
}
};
export default reducer;
REGIONS(reducer)
// import actions
import { SAVE_REGIONS } from '../actions/regions';
const initialState = {
regions: [],
};
const reducer = (state = initialState, action = {}) => {
switch (action.type) {
case SAVE_REGIONS:
/*
- on crée un nouvel objet : {}
- on déverse dedans toutes les informations du state actuel : ...state
- on écrase une valeur qui provenait du state actuel
*/
return {
...state, // regions: state.regions
regions: action.regions,
};
default:
return state;
}
};
export default reducer;
SRC INDEX.JS
// == Import : npm
import ReactDom from 'react-dom';
import { Provider } from 'react-redux';
import { BrowserRouter } from 'react-router-dom';
// == Import : local
// Components
import App from 'src/components/App';
import store from 'src/store';
// bootstrap's Css
import 'bootstrap/dist/css/bootstrap.min.css';
// == Render
// 1. Root React element (the one that contains the entire app)
// => Creates Nested Object Structure (Virtual DOM)
const rootReactElement = (
<Provider store={store}>
<BrowserRouter>
<App />;
</BrowserRouter>
</Provider>
);
// 2. The target of the DOM (where the structure is to come to life in the DOM)
const target = document.getElementById('root');
// 3. Trigger React (virtual) rendering =dom > (web page)
ReactDom.render(rootReactElement, target);
屏幕截图
What is the pb?
When I did a pull origin main, I recovered another branch with a reducer in addition to mine. So, I had a conflict with reducers. I created an index.js in reducers folder in order to combine both of them. In store redux, I call the index.js reducer but states aren't read in redux devtools.
CODE
STORE
import { createStore, applyMiddleware, compose } from 'redux';
import reducer from 'src/reducers/genres';
import regionsMiddleware from '../middlewares/regionsMiddleware';
import genresMiddleware from '../middlewares/genresMiddleware';
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
const enhancers = composeEnhancers(
applyMiddleware(
genresMiddleware,
regionsMiddleware,
),
);
const store = createStore(
// reducer
reducer,
// enhancer
enhancers,
);
export default store;
REDUCER(jndex)
import { combineReducers } from 'redux';
import genresReducer from './genres';
import regionsReducer from './regions';
const rootReducer = combineReducers({
genres: genresReducer,
regions: regionsReducer,
});
export default rootReducer;
GENRES(reducer)
import { SAVE_GENRES } from '../actions/genres';
const initialState = {
genres: [],
};
const reducer = (state = initialState, action = {}) => {
console.log(`le reducer a reçu une action ${action.type}`);
switch (action.type) {
case SAVE_GENRES:
/*
- on crée un nouvel objet : {}
- on déverse dedans toutes les informations du state actuel : ...state
- on écrase une valeur qui provenait du state actuel
*/
return {
...state, // list: state.list
genres: action.genres,
};
default:
return state;
}
};
export default reducer;
REGIONS(reducer)
// import actions
import { SAVE_REGIONS } from '../actions/regions';
const initialState = {
regions: [],
};
const reducer = (state = initialState, action = {}) => {
switch (action.type) {
case SAVE_REGIONS:
/*
- on crée un nouvel objet : {}
- on déverse dedans toutes les informations du state actuel : ...state
- on écrase une valeur qui provenait du state actuel
*/
return {
...state, // regions: state.regions
regions: action.regions,
};
default:
return state;
}
};
export default reducer;
SRC INDEX.JS
// == Import : npm
import ReactDom from 'react-dom';
import { Provider } from 'react-redux';
import { BrowserRouter } from 'react-router-dom';
// == Import : local
// Components
import App from 'src/components/App';
import store from 'src/store';
// bootstrap's Css
import 'bootstrap/dist/css/bootstrap.min.css';
// == Render
// 1. Root React element (the one that contains the entire app)
// => Creates Nested Object Structure (Virtual DOM)
const rootReactElement = (
<Provider store={store}>
<BrowserRouter>
<App />;
</BrowserRouter>
</Provider>
);
// 2. The target of the DOM (where the structure is to come to life in the DOM)
const target = document.getElementById('root');
// 3. Trigger React (virtual) rendering =dom > (web page)
ReactDom.render(rootReactElement, target);
Screenshots
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的商店中,您应该导入正确的减速机。
您现在这样做,
但可能应该是(因为您想使用组合的
rootReducer
)此外,由于您使用
combineReducers
实用程序,它只会将相关状态传递给您的特定减速器,因此您不应返回它们的完整路径。应该成为
并且
应该成为
In your store, you should import the correct reducer.
You now do
but should probably be (since you want to use the combined
rootReducer
)Additionally, since you use the
combineReducers
utility, it will only pass the relevant state to your specific reducers, so you should not return the full path from them.should become
and
should become