score:2
This is to do with the way react handles elements in its virtual DOM.
The Solution: Add a unique key (not using an index) and it will work.
<StatefulComponent param={"Tomato"} key={"Tomato"} />,
<StatefulComponent param={"Potato"} key={"Potato"}/>
Why does this behaviour happen?
By default, if no key is specified, react will give each component in a list a key with the value of the index order that it was initially rendered in - This is what is causing the issue. Because you haven't defined an id, React is implicitly making your components into this:
<StatefulComponent param={"Tomato"} key={0} />,
<StatefulComponent param={"Potato"} key={1}/>
And after the array reverse()
:
<StatefulComponent param={"Potato"} key={0} />,
<StatefulComponent param={"Tomato"} key={1}/>
The reason you see the unexpected behaviour is because react uses keys to identify each element. So if you change your array order after the original render then apply an index as the id on each render, react will get the element that was first rendered with that index as it's key and put it in that place. By changing the key to a unique identifier, react does not get the elements mixed up (since the unique identifier never changes with respect to its element) and can now accurately render each item in the order you intend.
Example: We render a list:
<div id=1>Foo</div> // Id "1" is now bound to this element
<div id=2>Bar</div>
So if you then reorder the list like this (note the id changes):
<div id=1>Bar</div> // this will be transformed to <div id=1>Foo</div>
<div id=2>Foo</div> // this will be transformed to <div id=2>Bar</div>
Then react will transform the elements into whatever id they were assigned to first - This is why it's important to have unique identifiers.
You can read more here: https://medium.com/@robinpokorny/index-as-a-key-is-an-anti-pattern-e0349aece318
Source: stackoverflow.com
Related Query
- How to change component order while keeping state?
- how to rerender a component when hash change and change the state
- Reactjs how to change the state of a component from a different component
- how and when to call a react component methods after state change from redux
- How to wait for a Redux action to change state from a React component
- React - How change state of single component when there are multiple instance of same component?
- How to update my useEffect hook when State change happen in a different .js file's component in ReactJS?
- How to get access to state with higher order component
- How to change the state of a sibling component in ReactJS for a Master/Slave checkbox system?
- How can I change a child's state from the parent component
- next.js how to change parent state from child component without triggering a button?
- How to optimize in react js while working with useReducer, looped over state which is passed to child component
- How to set a loading state for react component while sorting and filtering data?
- How to change the filter method of a higher order component in React?
- How do you change state of a react component from a different component?
- How can I change the state of a parent component from a child component?
- How to reset state in a component on prop change
- How to animate a conditionally rendered part of the component on state change using javascript and css?
- How to change the state of the particular component when it is clicked, using useState in react js?
- how to change component background color changing state in react.js?
- How can I change state of current map item in react component
- How to Change a css property based on a state of another component
- How can I get a component to update on path change while using the same route and component?
- In React, how can a parent change prop or state values on another component programatically?
- How do I change the state of the parent component from the child component in Reactjs?
- How to update React State in Functional Component while fetching data using API
- How to change the internal state property of a component in an action creator function
- How do I tell React component to change its state when an event is triggered from third party lib?
- how to prevent child component from reload when the parent state is change
- ReactJS - How to change a component's state based on another component
More Query from same tag
- MUI: Moving pagination to the top of DataGrid?
- Getting error while using React Router Dom
- Sails, React and Axios Error with CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource
- How to test if method was called inside componentDidMount?
- How to map through all children with Ant Design Tree Node
- React Router server rendering with server props
- React Redux: How to handle errors in RTK Queries/Mutation Typescript?
- How to test useRef with the "current" prop in jest/enzyme
- How to create Auxiliary (Higher Order Component) in preact?
- Stateless component: A valid React element (or null) must be returned
- My chartJS axios.get is fired twice causing chart to render twice too and i don't know why. I'm using react-chartjs-2
- Why does Redux saves my regex value as an object type
- How to set up dynamic environment configurations in React
- Are component events documented?
- How can I import another file from another file?
- Delay in updating D3 map in react
- How do I see modal by clicking image in react??(instagram clone coding)
- How can I achieve something like this image with chart.js(react-chartjs-2)?
- How to add margin to label using react and typescript?
- How to return the first object of array in reactjs
- MongoDB + Express cannot use a session that has ended
- Restarting setInterval after clearInterval in React
- How to use React.hydrate when using vue?
- Redux connect works but now for all properties from the state
- Change active element in a list using react
- How to enhance the response in apollo client with react only once
- AgGrid's cellRendererFramework doesn't have access to Material UI's theme
- How to remove duplicate arithmetic operators using regex
- Typescript React Redux - property does not exist on Action type
- How to provide a nested Generic in typescript?