�
�
­2019-01-26 16:58:49
JavaScript
­, 2019-01-26 16:58:49

Why re-render the left list (react-redux)?

I am quite new in the field of programming in general, so if this question seems stupid or stupid to someone, please understand and forgive me.
to the point.
I have this simple react application:

link to code and image
github
5c4c650a4d303668197981.jpeg

the goal is this:
when you click on a card in the left list, it is cloned into the right list.
like this
5c4c67145268b829690147.jpeg

state looks like this:
redux state
5c4c5eb567622135145689.jpeg

there is data - this is an object {} containing a complete list of cards.
there are cards - this is an array [] containing the id of the cards that are displayed in the left list.
there is a deck - this is an array [] to which id cards are added, which should be displayed in the right list.
and the shit is that when you click on the map in the left list, the extra components are re-rendered:
this can be seen with highlight updates
5c4c663c84218856481545.png

that is, only the right list is updated (card ids are added to it), but all other components are re-rendered.
why was the left list re-rendered if it does not change in any way?
even in diff you can see that only the deck array is changed:
diff
5c4c64a92a5e2237050851.png

ps somewhere I heard that a component that is connected via connect becomes a pure component by default, and is not updated if an updated value does not come to it. but for some reason it doesn't work for me.
// --------------------
this is what helped: I
deleted the node_modules folder that came by default with create-react-app and wrote npm install.
and everything went right

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
forspamonly2, 2019-01-27
@640

the cards in the left list are being redrawn because each of them is attached to the redux store. more precisely, Redux wrappers are redrawn, which are Context.Consumer - you probably saw them in React devtools.
the component itself is indeed pure by default, but it works like this: a redux wrapper catches the store change, gets a new store, calls your mapStateToProps , and re-renders the wrapped component with the new props. and after that it executes shouldComponentUpdate from the internal PureComponent, understands that nothing has changed and the wrapped component itself is no longer redrawn.
globally, you should make the card a dumb (presentational) functional component - accept a ready-made card data object for rendering in it, and receive and pass it from the connected list component.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question