I
I
Itvanya2016-08-08 11:25:32
JavaScript
Itvanya, 2016-08-08 11:25:32

How to organize Redux code to get properties from a third party reducer to get its state?

Friends, hello There was a problem: there is an initial state in which there are only two fields. The first field is an object that contains a sheet that is loaded from the server. Conventionally, there is data with information about some people. The second field is dynamic data, which indicates the data of the person who was clicked on in the list (component) of people.
The bottom line is that since this data changes dynamically, they are assigned from persons.list by sorting through it by ID. There are two reducers - one for each state object. The task is to get the state of one reducer from another. How can this be implemented?

// Изначальное состояние

const initialState = {
  persons : {
    list : [{pers1}, {pers2}],
    fetched : false,
    fetching : false,
    error : null
  },
  currentPerson : {
   id : 777,
    name : 'Andreas',
    surname : 'Smith',
    clicks : 100500
  }

// Редьюсеры. Редьюсеру currentPersonReducer нужно получить доступ к данным из
// состояния, которое сгенерировал нам personReducer, чтобы сделать 
// итерацию по массиву и задать состояние текущего человека.

function currentPersonReducer(state = initialState.currentPerson, action) {
  switch(action.type) {
    case 'SET_PERSON':
       //Здесь нужно получить доступ к данным редюсера
  }
}

function personReducer(state = initialState.person, action) {
  switch (action.type) {
    case 'ADD_PERSON':
      var personObj = action.payload;
      return Object.assign({}, state, {
        list : state.list.concat(personObj)
      });
  }
}
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Nikolai Antonov, 2016-08-18
@my-nickname

It is necessary to store data in one reducer. Don't split by 2 - less code and easier.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question