Redux:从 reducer 存储的方法
Redux: Approach to store from reducer
我正在尝试从减速器中获取商店。
我看到 redux 架构不支持 reducer 之间的共享。
但在我的情况下确实需要它。
const initState = {
schemas: [],
};
const myReducer = (state , action) => {
state = state || initState;
switch (action.type) {
case 'redux-form/CHANGE':
const schemaId = getStore().context.schema;
let modifier = state.schemas.get(schemaId);
modifier[action.key] = action.value;
return {
...state
};
}
};
我的应用减速器:
const iceApp = combineReducers({
form: formReducer,
context,
myReducer,
});
先谢谢了。
您可以将 reducer 函数添加到状态的任何级别,请考虑:
const complexReducer = (state, action) {
const {context, myReducer} = state;
switch (action.type) {
case 'redux-form/CHANGE':
// do something with myReducer
return {context, myReducer};
default:
return state;
}
}
// a simple utility function to call reducers in sequence on the same state
function composeReducers(...reducers) {
return (state, action) => reducers.reduceRight((acc, reducer) => reducer(acc, action), state);
}
const iceApp = composeReducers(complexReducer, combineReducers({
form: formReducer,
context,
myReducer,
}));
这会将 complexReducer
应用于来自简单减速器的整个状态。
另一种方法是在操作中访问 context
并将其作为操作的负载传递。
const changeAction = ... // the redux-form/change action
const changeActionWithContext = () => (dispatch, getState) => {
const state = getState();
const schemaId = state.context.schema;
dispatch(changeAction(schemaId));
}
我现在 redux-form
所以我不知道是否可以将自定义负载添加到 redux-form
操作。
我正在尝试从减速器中获取商店。 我看到 redux 架构不支持 reducer 之间的共享。 但在我的情况下确实需要它。
const initState = {
schemas: [],
};
const myReducer = (state , action) => {
state = state || initState;
switch (action.type) {
case 'redux-form/CHANGE':
const schemaId = getStore().context.schema;
let modifier = state.schemas.get(schemaId);
modifier[action.key] = action.value;
return {
...state
};
}
};
我的应用减速器:
const iceApp = combineReducers({
form: formReducer,
context,
myReducer,
});
先谢谢了。
您可以将 reducer 函数添加到状态的任何级别,请考虑:
const complexReducer = (state, action) {
const {context, myReducer} = state;
switch (action.type) {
case 'redux-form/CHANGE':
// do something with myReducer
return {context, myReducer};
default:
return state;
}
}
// a simple utility function to call reducers in sequence on the same state
function composeReducers(...reducers) {
return (state, action) => reducers.reduceRight((acc, reducer) => reducer(acc, action), state);
}
const iceApp = composeReducers(complexReducer, combineReducers({
form: formReducer,
context,
myReducer,
}));
这会将 complexReducer
应用于来自简单减速器的整个状态。
另一种方法是在操作中访问 context
并将其作为操作的负载传递。
const changeAction = ... // the redux-form/change action
const changeActionWithContext = () => (dispatch, getState) => {
const state = getState();
const schemaId = state.context.schema;
dispatch(changeAction(schemaId));
}
我现在 redux-form
所以我不知道是否可以将自定义负载添加到 redux-form
操作。