Redux: condition conditioner condition

As I understand it, action creators have no idea about the state, and reducers are not allowed to start new actions. So, how do you define an action based on state - does it need to be done in the render function?

As an example, I have a component that lists the names for a set of elements. This data is stored in state.matches. By clicking on an item, you should open a detailed view in my component Entries.

I currently have this action creator that downloads a record and runs an action addEntrythat causes the relay to Entriesadd data to the cache in state.entries.

export function getEntryFromId(id) {
  return function(dispatch) {
    dispatch(lookupInit(id));

    fetch('/register/id/' + id)
      .then( res => res.json() )
      .then( res => dispatch(addEntry(res[0])) )     // data returned as array of one element
      .catch( err => dispatch({ type: ENTRY_LOOKUP_FAIL }) );
  }
}

Currently, it is always loading new data because it cannot check the cache.

id SHOW_THIS_NEXT , , , . .

.

+4
1

, Redux Thunk thunk getEntryFromId? getState:

export function getEntryFromId(id) {
  return function(dispatch, getState) {
    const currentState = getState();
    // do something with `currentState`,
    // only dispatch if necessary
  }
}
+5

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


All Articles