The method of calling child components by react parent component

I want to call the method of the subcomponent, but I can"t find the method of the subcomponent when printing.

Mar.11,2021

you use shouldhide on state to show different parts.

when shouldhide is true , the second half is not displayed. So you can't get this.refs.test .

also, if you want to do something related to state after calling setState , you should wait until setState is finished.

because setState is an asynchronous operation, you cannot follow the Synchronize operation on the figure. It should look like this:

this.setState({
  shouldhide: true
}, () => {
  console.log(this.refs.test)
})

but in fact, you can get this.refs.test only if shouldhide is false .

modify BasicEdit to the following:

BasicEdit = () => {
    this.setState({
        shouldhide: true
    }, () => {
        setTimeout(() => {
            console.log(this.refs.test)
        }, 500)
    })
}

shouldhide when you set it to false, you render the data in if, but there is no ref in if

Menu