Trigger child re-rendering in React.js

后端 未结 5 1046
自闭症患者
自闭症患者 2020-12-24 01:38

The Parent (MyList in my example) component renders an array thru a Child (MyComponent) component. Parent decides to change properties in the array

5条回答
  •  旧巷少年郎
    2020-12-24 02:24

    I have found a nice solution using key attribute for re-render with React Hook. 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 of 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 (
        
          {data.map((item) => (
            
          ))}
        
      );
    };
    
    export default BucketInput;
    

提交回复
热议问题