How do I unload and reload a child component after the React parent component triggers an event?

problem description

A popular situation in which the parent component updates the render and causes the child component to render again

but now there is a special requirement: there is a button on the parent component to refresh real-time data, and I hope I can uninstall this child component and reload it when I click Refresh. This initializes the state value of the subcomponent.

the environmental background of the problems and what methods you have tried

Specifically, the sub-component changes the value of its state when it is rendered for the first time, and there is an array parameter array in state that I use to judge certain events, which is empty at first, and will continue to fill in some values, while I trigger some events through array.includes (xxx) &. . If I don"t clear the value of state when I click the refresh button, Events that follow & & can never be triggered, resulting in real-time data updates

.

related codes

/ / Please paste the code text below (do not replace the code with pictures)
I will briefly simulate the situation

//
const { datasource } = this.state;
render(){
    ...
    return(
       <ChildComp data={datasource}></ChildComp>
    )
}

//
class ChildComp extends PureComponent{
    constructor(arg){
        super(arg)
        
        this.state = {
            type: []
        }
    }
    
    componentDidMount(){
        this.handleData();
    }
    
    componentWillProps(){
        this.handleData();
    }
    
    handleData(){
        const { type } = this.state;
        const { data } = this.props;
        
        !type.includes(data.type) && type.push(data.type) && ... 
    }
}

when I first loaded, I had already filled the data for the child component ChildComp . When the parent component refreshes and triggers the child component willProps re-render, the value of this.state.type already exists data.type , so that subsequent actions cannot be triggered. Is there any solution?

what result do you expect? What is the error message actually seen?

is there an operation in which child components can be reloaded in the parent component?

//
handleRefresh(){
    //<ChildComp />
}

or is there a better way to solve it?

Thank you!

Oct.07,2021

think too much. Instead of passing values in an array, you should use {} instead of array methods such as push. The logic is much simpler.


there is a life cycle called shouldComponentUpdate. I don't know if it can solve your problem


set a key for the subcomponent and change the key value when the subcomponent needs to be unloaded, but I don't think your problem should be implemented by unloading the subcomponent.


Let me tidy up the logic. You want the child component to get the changed data of the parent component when the parent component data changes, and then integrate it into the state of the child component, triggering the child component to re-render.
solution:

  • construct talk about props data integration into state
  • componentWillReceiveProps integrate into state again

there are a few things I don't understand about your code

  1. What is the method of componentWillProps ? Is it componentWillReceiveProps ? If so, this.props should not be used in handleData
  2. componentWillReceiveProps Why not use
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b2c590-2bafd.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b2c590-2bafd.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?