A
A
Archakov Dennis2018-03-28 23:49:52
React
Archakov Dennis, 2018-03-28 23:49:52

ReactJS: Why doesn't the component update when the store changes?

When adding a comment, the component is not updated for some reason. There are 2 reducers.
questions - stores a list of questions, each of which contains a property (array) with answers to questions.
fullQuestion - information about a specific question.

import { compose, withHandlers } from 'recompose';
import { connect } from 'react-redux';
import find from 'lodash/find';

const mapStateToProps = ({ questions }, { match }) => ({
  question: find(questions.items, { id: parseInt(match.params.id) })
});

export default compose(
  connect(mapStateToProps, mapDispatchToProps),
  withHandlers({
    ...
  }),
)(FullQuestion);

When I send an action to add a response, the changes are made, but the component is not updated.
const initialState = {
  items: [],
};

const questions = (state = initialState, action) => {
  switch (action.type) {
    case 'QUESTIONS:ADD_ANSWER':
      return {
        ...state,
        items: state.items.map(obj => {
          if (obj.id === parseInt(action.payload.question_id)) {
            obj.answers = [...obj.answers, action.payload];
          }
          return obj;
        })
      };

    default:
      return state;
  }
};

export default questions;

5abbffcb77c56237639183.png
The response is stored in question.answers, but the component is not updated.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Spirin, 2018-03-29
@archakov06

You are passing the same object to the component. Here it is not updated. Your component is not interested in changing the storage, only changing the data that you pass there.
Store flat data. Then such problems will not arise.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question