最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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 ??

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 ??

Share Improve this question edited Jun 22, 2018 at 7:08 jadlmir asked Mar 6, 2018 at 10:26 jadlmirjadlmir 5051 gold badge7 silver badges16 bronze badges
Add a comment  | 

6 Answers 6

Reset to default 3

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

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
  }
}

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.

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?

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;

You can use useState

e.g.

export function myChildComponent({myValue}) {
   const [MyValue, setMyValue] = useState(null);
   useEffect(() => {
       setMyValue(myValue);
   }, [myValue])
}
发布评论

评论列表(0)

  1. 暂无评论