如何在 React 组件的 return 函数中使用 IIFE?

How can I use an IIFE in the return function of a react component?

当用户单击按钮时,我会弹出一个模态页面,它运行良好:

render() {
  return (
     <div>
         <section>
             <button onClick={() => this.refs.simpleDialog.show()}>Open Modal</button>
         </section>
         <SkyLight hideOnOverlayClicked ref="simpleDialog" title="Test Modal">
             Text that appears inside the modal page
            <Button onClick={() => this.refs.simpleDialog.hide()} >Got It</Button>
         </SkyLight>
    </div>
)}

问题:

用于模态的库: https://github.com/marcio/react-skylight

要在组件安装时打开模型,只需将 isVisible 设置为 true

<SkyLight isVisible={true} ref="simpleDialog" title="Test Modal">

我认为您正在寻找的是 componentDidMount() 生命周期方法:

componentDidMount() {
    this.refs.simpleDialog.show();
}

来自React docs

componentDidMount() is invoked immediately after a component is mounted. Initialization that requires DOM nodes should go here. If you need to load data from a remote endpoint, this is a good place to instantiate the network request. Setting state in this method will trigger a re-rendering.

随时查看其他 component lifecycle methods