简体   繁体   English

React 条件渲染:(a)等待孩子被挂载

[英]React conditional rendering: (a)wait for children to be mounted

I have a React class component that renders null by default, and some children after an activate() function is called.我有一个 React class 组件,默认情况下呈现null ,并且调用了activate() function 之后的一些子项。 Roughly like this:大致是这样的:

class MyComponent extends React.Component {

    ...

    activate() {

        this.setState({showComponent: true})

    }

    ...

    render() {
        if (this.state.showComponent) {
            return <Child />
        } else {
            return null
        }
    
    }
}

I have an external JavaScript script in which I interact with the MyComponent (call activate function).我有一个外部 JavaScript 脚本,我在其中与MyComponent交互(调用激活函数)。

To keep it short, my problem is that after calling activate() in this external JS script and trying to access HTMLElements in the <Child /> component right after (with document.getElementById ) I get null pointers as the <Child /> component is rendered asynchronously a bit later.简而言之,我的问题是在此外部 JS 脚本中调用 activate() 并尝试访问<Child />组件中的 HTMLElements 之后(使用document.getElementById )我得到 null 指针作为<Child />组件稍后异步呈现。

Is there a way to adapt the activate() function in MyComponent to 'wait' for all children in <Child /> to be mounted?有没有办法调整MyComponent中的activate() function 以“等待” <Child />中的所有子级挂载?

I already tried to exploit the async nature of setState and tried async activate() with await this.setState({showComponent: true}) but this did not change the rendering order.我已经尝试利用 setState 的异步特性并尝试使用await this.setState({showComponent: true})进行async activate()但这并没有改变渲染顺序。

So in short, is there a way to wait for children to be rendered after you perform a setState update.所以简而言之,有没有办法在你执行 setState 更新后等待孩子被渲染。 I guess this is a bit of a special case as it involves conditional rendering and MyComponent initially renders nothing.我想这有点特殊,因为它涉及条件渲染,而MyComponent最初什么也不渲染。

Happy about any ideas::)对任何想法感到高兴::)

How about emitting a custom event on window object inside componentDidMount of Child component and listening for this event in the external javascript and accessing the HTML element in the event handler. How about emitting a custom event on window object inside componentDidMount of Child component and listening for this event in the external javascript and accessing the HTML element in the event handler.

we can pass a function to child component which we can call in child's componentDidMount hooks, so when the child gets mounted, it will trigger that function... but keep in mind don't update any state variable of parent component in that passed function otherwise it will stuck in a loop (coz as the state variable changes, it will re- render all the child component and eventually component did mount will also be called...) we can pass a function to child component which we can call in child's componentDidMount hooks, so when the child gets mounted, it will trigger that function... but keep in mind don't update any state variable of parent component in that passed function否则它将卡在一个循环中(因为 state 变量发生变化,它将重新渲染所有子组件,最终组件确实 mount 也将被调用......)


    class MyComponent extends React.Component {

        ...

        activate() {

       this.setState({showComponent: true})

    }

        callback = ()=>{
       // function to be passed in child component 
      // don't update any state variable here .. 
        }

      ...

     render() {
      if (this.state.showComponent) {
        return <Child callback={this.callback} />
     } else {
         return null
     }

  }
}

  class child extends React.Component {
    constructor (props){
    super(props)
 }

   componentDidMount(){
     this.props.callback() // this will be called when this child will be 

mounted }挂载}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM