1

In my React project I have Redux state that is shaped something like this:

{
  items: [{ ... },  { ... }, { ... }, ...]
  form: { 
    searchField: '',
    filter1: '',
    filter2: '',
  }
}

On one page there is a form component and list of items component. I am using Immutable.js, so a brand new items array is created whenever a form field is updated. Because the array reference is now different, this causes the list of items to be re-rendered even though the data within the array is the same.

Within my list component I am using componentDidUpdate to console log changes in state to confirm that the updated array is triggering the re-render:

componentDidUpdate(prevProps) {
  Object.keys(prevProps).forEach((key) => {
    if (prevProps[key] !== this.props[key]) {
      console.log('changed prop:', key, 'from', this.props[key], 'to', prevProps[key]);
    }
  });
}

The from and to arrays that get logged contain all the exact same data.

How can I prevent these unnecessary re-renders?

The only thing I can think of is, within the shouldComponentUpdate of the list component, loop over the array and compare the item IDs with nextProps. However, this seems inefficient.

Here is my reducer

import Immutable from 'immutable';
import { types } from '../actions';

const initialState = {
  items: []
  form: { 
    searchField: '',
    filter1: '',
    filter2: '',
  }
}

export default function reducer(_state = initialState, action) {
  const state = Immutable.fromJS(_state);

  switch (action.type) {
    case types.HANDLE_FORM_CHANGE:
      return state.setIn(['form', action.field], action.value).toJS();
    default:
      return _state;
  }
}
2
  • could you share your redux reducer? this looks like you could have an issue there. Commented Jan 17, 2019 at 23:08
  • Usually this isn't something that you prevent within the redux cycle. Sometimes a restructure of the data structure that holds the data is neccessary, but that isn't the solution you should jump straight to. Show us your components that are related to this data Commented Jan 17, 2019 at 23:24

1 Answer 1

1

The shouldComponentUpdate approach is the way I've handled this / seen others handle this.

Note that you don't need to manually loop over your collection and compare ids as Immutable includes an is method for comparing collections:

Value equality check with semantics similar to Object.is, but treats Immutable Collections as values, equal if the second Collection includes equivalent values.

Obviously this assumes the state passed to your components are Immutable objects which I understand to be considered something of a best practice (see use immutable everywhere except your dumb components).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.