0

being new to redux, I came across the following challenge: I have a filter-list, which must be updated both after the first load of a component, and every time someone changes a dropdown in the top navbar. The filter-list is dependent on another dropdown which is part of my state. A pseudocode is below [1].

I could theoretically duplicate my action dispatch in both Components (the main one and the navbar), but this is bad design of course.

This brings me to the question: Where does one put redux dispatch calls which must access the state themselves? Would this live in the action-file which would then need to access the state itself (which might not be good design on its own)?

Thanks!

[1]

function getDrillOptions() {
  let data = props.data; // <- is part of redux' store
  let countries = props.selectedFilters.countries; //<- part of redux' store
  let regions = getRegionForCountry(countries);
  let selectable = new Set();
  Object.keys(data[regions][countries]).map((weeknum) => {
    data[regions][countries][weeknum].map((row) => {
      selectable.add(row[event.target.value]);
    });
  });
  dispatch(populateFilters({filter: 'mainDrillOptions', value: [...selectable]})); // <- sets state
}
Bishonen_PL
  • 1,400
  • 4
  • 18
  • 31
  • Are you using `thunk` middleware? If yes, then use `getState` helper. If not, you can directly import the `store.js` and then use `store.getState`. For both the cases, the call would have to be in action file. – Utsav Patel Apr 20 '20 at 13:02
  • https://stackoverflow.com/questions/35667249/accessing-redux-state-in-an-action-creator This might help. – Utsav Patel Apr 20 '20 at 13:05

0 Answers0