score:7
your codepen is alomost fine, only thing you missed is proper key for message component,
for react to render components properly each component should have different key, so i changed the piece of code from
<Messages style="me" msg={this.state.chat} key={this.state.chat} sent={(this.state.sent[this.count])?this.state.sent[this.count]:''} />
To
<Messages style="me" msg={this.state.chat} key={this.count} sent={(this.state.sent[this.count])?this.state.sent[this.count]:''} />
and code-pen worked fine
EDIT this solves the propblem
instead of saving the components to state, dynamically build them. check this codepen.io/abhirathore2006/pen/JbjjPx
score:0
componentWillReceiveProps should take nextProps.
score:0
Whenever the whole component rerenders, React matches all the components with the previous state/key. So if you give a unique key to the child on every time parent rerenders, the child will also re-render.
Lodash provides a function _.uniqueId() that generates a unique id every time it is called.
<Parent>
<Child key={_.uniqueId('child_component')} />
</Parent>
score:1
Use this as a key for component
this.state.messages.length
React won't allow you to enter two elements with the same key. This is how the react virtual DOM diffs the changes on re-render
Source: stackoverflow.com
Related Query
- React - How do i force child components to re render when parent state component changes?
- How to stop re render child component when any state changed in react js?
- How does React re-use child components / keep the state of child components when re-rendering the parent component?
- How to avoid rerender all child components which in loop when parent component state update
- How to update state of child component when it get called in map function from parent component React JS
- How to re render parent component when anything changes in Child Component?
- How to update (re-render) the child components in React when the parent's state change?
- How to optimize React components with React.memo and useCallback when callbacks are changing state in the parent
- Stop Rerendering Child Components When Parent State Changes in React
- How to update parent state from child component in React + send a paramater
- How to Prevent Child Component Re-Render when Parent state changes
- How can I access child components values from a parent component when they are added dynamically?
- how to set react parent component state by a async function and then pass this state to child as props?
- react stateless child components do not update on state change in parent component
- How to pass input values from a child Form component to the state of its parent component for submission with react hooks?
- How to pass a state to a Parent component if I have a button triggering the state in the 2nd Child in React
- How do I pass a prop to a react component yet not update that prop in the child when parent changes?
- How to maintain state of child components when they are filtered through the parent component?
- How to refresh components when parent state changes in react when using react router
- React child component overtakes state from another child when filtered from parent
- How can reset a React child component back to original state after it updates state in the parent
- How to check state of a child component in react in render function?
- How to get parent props in child component when using styled component in react
- How to render a child component within parent component using react and typescript?
- React - Where to unit test state change when parent component passes method as prop: parent or child component?
- How to render component conditionally when leaf value of global state changes using react hooks
- Child component causing too many renderings on parent component when changing global state in React
- How to pass latest state to a Parent component from Child Component in react
- React Sort Child Components of Parent Component based on state in Child
- React Re-renders When Parent State is Changed Through Child Component
More Query from same tag
- Fetch runs double with old and updated value
- why is my router not working in react - React.js
- How do i render data from local storage in react typescript?
- React - Preference: function component vs class component
- Understanding React Mounting/Unmouting and page reloading
- React useState working other then expected
- How can I fetch data from yelp api in React JS?
- Can't set headers after they are sent in my react api
- React rendering black screen without errors
- How do I set css style in states on React?
- react-loadable exception 'expected a string (for built-in components) or a class/function (for composite components) but got: object'
- How to insert additional element to third-party component?
- How to reset useState everytime I change my page?
- Next.js state change not re-rendering UI
- Not able to get namedExports for ReactDOM in commonJS plugin in rollup
- Material-UI: the Select component doesn't accept a Fragment as a child. Consider providing an array instead
- Using props inside render method as a condition for changing UI?
- ReactJS passing Stateful variables to child components
- Conditional Check of Props Not Working In Styled-System
- How to overwrite the default style of a third party react component?
- How to access ReactQuill api?
- Programmatically set params in React Router v4
- Module not found - React
- React hooks not working in gatsby productioin build
- React Router With Hooks-Unable to navigate to screens
- ReactJS form validation Swedish Social Security Number
- How can I remove an array from my redux store?
- What does 'as' mean when importing a module?
- MERN pagination and filtering in same controller
- How to get error message in terminal when using Redux React