How can i only re-render the new child component when mapping an array from Redux state?

douwebsp

Context

I have a Redux state in which i have an array of items. Everytime you click a link in the menu, an item gets added to this array.

In one of my components, which is connected to the store using connect(mapStateToProps)(Component) I map through this array and load the child components. These child components fetch data from an API.

Problem

Everytime a new item gets added to the array, all the child components re-render, because the array that it maps changes. I only want the newly added item to render and the rest persist as they are. I know you can do something with useMemo, but I'm not sure how to correctly use that with mapping.

Thanks to anyone who can help me!

HMR

You can use pure component for each item:

const id = ((id) => () => id++)(1);

const Item = React.memo(function Item({ item, increase }) {
  const rendered = React.useRef(0);
  rendered.current++;
  return (
    <li>
      {item.id} - rendred {rendered.current} count:{' '}
      {item.count}
      <button onClick={() => increase(item.id)}>
        increase count
      </button>
    </li>
  );
});

const App = () => {
  const [items, setItems] = React.useState([]);
  const addItem = () =>
    setItems((items) => [{ id: id(), count: 0 }, ...items]);
  //use useCallback to create an increase function that
  //  never changes so you'll never pass a new increase
  //  function to item
  const increase = React.useCallback(
    //pass callback to state setter so items is not
    //  a dependency of the useCallback
    (id) =>
      setItems((currentItems) =>
        currentItems.map((item) =>
          item.id === id
            ? { ...item, count: item.count + 1 }
            : item
        )
      ),
    []
  );
  return (
    <div>
      <div>
        <button onClick={addItem}>+</button>
      </div>
      <div>
        {items.map((item) => (
          <Item
            key={item.id}
            item={item}
            increase={increase}
          />
        ))}
      </div>
    </div>
  );
};

ReactDOM.render(<App />, document.getElementById('root'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.4/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.4/umd/react-dom.production.min.js"></script>


<div id="root"></div>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

React - How do i force child components to re render when parent state component changes?

How can I get all sibling components to correctly re-render when dispatching a redux action from one sibling component?

How to re-render a child component when new data arrives from the parent

How can I reRender Child Component from Main Component when state changes in RN 0.61?

How can I update the state from parent component to child component when its passed as a prop?

React when parent component re-render, I loose child state. What I'm missing?

How can I change the state of a parent component from a child component?

React Native: How to change parent component's state from child component and re-render parent?

Which React cycle method should I use to re-render component when redux state has changed?

How to re-render checkbox state when mapping over state

Re-Render From Child Component After State Change

How to get updated redux-toolkit state when component is not re-render

How to not re-render a child component when a parent's state changes in React?

How to Prevent Child Component Re-Render when Parent state changes

How to stop re render child component when any state changed in react js?

How can I dispatch a state change to redux when calling it from a function inside the component?

how to re-render child component on state change(Parent) reactjs

re render a react component only when the state change

How to re render parent component when anything changes in Child Component?

How do I force parent component re render from child component

How to re-render only one component on hover when state changes in React

How to re-render parent component from child component

How can i render a route component in the parent component from a child component

React: How to Child component re-render only prop update

React Redux-a child component does not re-render on state change

How can i remove an object from an array state in Redux/react

How does a redux connected component know when to re-render?

How to re-render vue component when vuex state change?

How can I render Parent component again after update from the child component