How can I share readonly state between two or more gearboxes

I need access to an editable user state from two or more reducers. Is there a way to access the state controlled by another gearbox without transferring it to the gearbox through the action payload? I want every action to not send custom settings to reducers.

State

{
  userSettings: {
    someSetting: 5
  },
  reducer1State: {
    someValue: 10 // computed with userSettings.someSetting
  },
  reducer2State: {
    someOtherValue: 20 // computed with userSettings.someSetting
  }
} 

From gear 1, I would like to get userSettings.someSettingusing something like the following:

function update(state={}, action) {
  if (action.type === constants.REDUCER_1.CALCULATE) {
    return _.assign({}, state, {
      someValue: 2 * GETSTATE().userSettings.someSetting
    });
  }
...

I do not want to have to send userSettings from the action as follows:

export function calculate(userSettings) {
  return {
    type: constants.REDUCER_1.CALCULATE,
    userSettings: userSettings
  };
}
+4
source share
1 answer

Redux , , , , , . , , , .

, , Reselect memoized , connectStateToProps, , - :

const getSomeSettings = state => state.userSettings.someSetting;
const getMultiplier = state => state.reducer1.multiplier;

const getSomeValue = createSelector([getSomeSettings, getMultiplier],
    (someSetting, multiplier) => {

});

const mapStateToProps(state) => {
    return {
        someValue: getSomeValue(state)
    }
}

const MyConnectedComponent = connect(mapStateToProps)(MyComponent);
+5

Source: https://habr.com/ru/post/1628601/


All Articles