Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have a modal page popping up when the user clicks a button, it's working perfectly :

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>
)}
  • But My goal is to open the modal automatically when the user opens the page for the first time.

  • I don't want to open the modal page by clicking on a button

Question:

  • Can I use an IIFE (An immediately-invoked function expression) in order to open the modal as soon as the user open the page ?

  • My approach was to set a boolean to true. Then open the modal if the value is set to true

Library being used for the modal : https://github.com/marcio/react-skylight

like image 947
AziCode Avatar asked Jan 01 '17 02:01

AziCode


People also ask

Where can I use IIFE?

One typical use case of an IIFE and closure combination is to create a private state for any variable. In the above example, the secret value can only be changed by using the method as the variable is not available globally and is discarded off as soon the IIFE gets executed.

What does a render () method in Reactjs return?

Purpose of render(): React renders HTML to the web page by using a function called render(). The purpose of the function is to display the specified HTML code inside the specified HTML element. In the render() method, we can read props and state and return our JSX code to the root component of our app.

How do you call a function in IIFE?

checkCode("Zorg!"); The IIFE returns an object with the checkCode property, that property is the (private) function. The point of the IIFE is that this scopes the variables and functions within, so that they are not accessible from outside (e.g. privateCheckCode and secretCode exist only inside the IIFE).

Can a function return JSX in React?

react/no-jsx-outside-component: If a function returns JSX, it should be a React component (class or function). The rule would check all functions in a file: if the function returned JSX. or if the function returned React.


2 Answers

I think what you're looking for is the componentDidMount() lifecycle method:

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

From the 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.

Feel free to checkout other component lifecycle methods.

like image 108
Douglas Ludlow Avatar answered Sep 19 '22 07:09

Douglas Ludlow


To have a model open on component mount, just set isVisible to true

<SkyLight isVisible={true} ref="simpleDialog" title="Test Modal">
like image 41
Andy Ray Avatar answered Sep 20 '22 07:09

Andy Ray