Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have an array list
logged in the child component which returns me the initial array.
After changing data from the API componentDidMount
I get an array of objects
if i log that array in the Parent component in the render function.
it is changing
but the child component it is not.
what shall i do ??

like image 472
jadlmir Avatar asked Mar 06 '18 10:03

jadlmir


People also ask

Do child components render when parent state changes?

State changes in Child component doesn't effect on the parent component, but when a state of parent component changes all the child components render.

How do you force re-render child component React?

To force the child component to re-render — and make a new API call — we'll need to pass a prop that will change if the user's color preference has changed. This is a simple switch we can flip.

Does changing state re-render component?

When the state changes in the parent component (in this case, App ), the two Tile components will re-render, even though the second one doesn't even receive any props. The red dots again represent renders. In React, this means calling the render function.


5 Answers

There are a few ways to do this but you basically need to tell your child component that the props have updated.

One way to do this would be to use shouldComponentUpdate

shouldComponentUpdate(nextProps, nextState){
    return this.props.items[0] !== nextProps.items[0]);
}

You may want to have some better checking in you if statement to see if the array has changed but I think you get the idea.

You could also use componentDidUpdate or componentwillreceiveprops but they are used if you are updating state which will force a re-render

like image 141
Stretch0 Avatar answered Oct 04 '22 01:10

Stretch0


You should use the component lifecycle method, componentWillReceiveProps. componentDidMount is only called once after the component has mounted. componentWillReceiveProps is always called when the props change. For reference, visit: componentWillReceiveProps. It'll be something like this:

componentWillReceiveProps(nextProps) {
  if(this.props !== nextProps){
    // your code here
  }
}
like image 40
Arslan Tariq Avatar answered Oct 04 '22 00:10

Arslan Tariq


Generally react rerenders child component automatically when there is a state change in parent component. I created this jsfiddle which works totally fine.

One reason the Array is not updating in the child component might be because the you are simply console logging the array and not using it to DOM.For that you can try using content in the array in the child component like simply

return (
    <div>this.props.Items[0].name</div>
)

So this might be once of the case.

But still if you want the console.log() to be printed without using the array elements in the child component then you can try

componentDidUpdate() {
    this.forceUpdate();
}

So whenever you are setting new state or updating the data, componentDidUpdate will be called there you can try to force rerender the component.Still untested.

like image 28
pritesh Avatar answered Oct 04 '22 01:10

pritesh


React itself re-renders a component and its child components whenever a state is changed. You don't need to do it by yourself. However, you can decide whether to update the component or not by using shouldComponentUpdate().

Check Does render get called any time “setState” is called?

like image 39
Amanshu Kataria Avatar answered Oct 03 '22 23:10

Amanshu Kataria


I have found a nice solution using key attribute. If we changed key property of a child component or some portion of React Component, it will re-render entirely. It will use when you need to re-render some portion of React Component or re-render a child component. Here is a example. I will re-render the full component.

import React, { useState, useEffect } from "react";
import { PrEditInput } from "./shared";

const BucketInput = ({ bucketPrice = [], handleBucketsUpdate, mood }) => {
  const data = Array.isArray(bucketPrice) ? bucketPrice : [];
  const [state, setState] = useState(Date.now());
  useEffect(() => {
    setState(Date.now());
  }, [mood, bucketPrice]);
  return (
    <span key={state}>
      {data.map((item) => (
        <PrEditInput
          key={item.id}
          label={item?.bucket?.name}
          name={item.bucketId}
          defaultValue={item.price}
          onChange={handleBucketsUpdate}
          mood={mood}
        />
      ))}
    </span>
  );
};

export default BucketInput;
like image 41
Tofazzal haque Avatar answered Oct 04 '22 01:10

Tofazzal haque