Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Immediately-invoked function expression inside JSX

I am working on the React project where I am trying to compile but cannot find why I am getting this syntax error. Specifically, what the pattern, "{()=>{}()}", is doing in this context?

Module build failed: SyntaxError: Unexpected token, expected } (35:9)

33 |             return (<div className="loading" />);
34 |           } 
35 |         }()}
   |          ^
36 |       </div>
37 |     );
38 |   }

@ ./src/containers/SearchApp.js 7:0-52
@ ./src/containers/App.js
@ ./src/index.js
@ multi ./src/index

The part of code:

render() {
return (
  <div>
    <div className="form-group">
      <input onKeyDown={this.searchEnter.bind(this)} type="text" ref="tag" className="form-control input-lg search-bar" placeholder="Enter the tag..." />
      <button onClick={this.searchClick.bind(this)} type="button" className="btn btn-default search-button">Search!</button>
    </div>
    {()=>{
      if (this.props.status === 'PENDING') {
        return (<div className="loading" />);
      }
    }()}
  </div>
);
like image 262
tak Avatar asked Aug 01 '17 05:08

tak


People also ask

What is the use of immediately invoked function expression?

An Immediately-invoked Function Expression (IIFE for friends) is a way to execute functions immediately, as soon as they are created. IIFEs are very useful because they don't pollute the global object, and they are a simple way to isolate variables declarations.

Which JSX You can write expression inside?

You can put any valid JavaScript expression inside the curly braces in JSX. For example, 2 + 2 , user. firstName , or formatName(user) are all valid JavaScript expressions. In the example below, we embed the result of calling a JavaScript function, formatName(user) , into an <h1> element.

What is an IIFE immediately invoked function expression )? Can you give an example?

An Immediate-Invoked Function Expression (IIFE) is a function that is executed instantly after it's defined. This pattern has been used to alias global variables, make variables and functions private and to ensure asynchronous code in loops are executed correctly.

What is immediately invoked function in JavaScript with example?

An IIFE (Immediately Invoked Function Expression) is a JavaScript function that runs as soon as it is defined. The name IIFE is promoted by Ben Alman in his blog.


1 Answers

This is Immediately-invoked function expression.

Error With your code?

You need to wrap the function inside (), Check this:

{
   (() => {
      if (this.props.status === 'PENDING') {
         return (<div className="loading" />);
      }
   })()
}

what the pattern, "{()=>{}()}", is doing in this context?

Directly we can't put if/else statement or any other statement inside JSX, so for that we need to create a function and put all the logic inside that.

As per DOC:

if-else statements don't work inside JSX. This is because JSX is just syntactic sugar for function calls and object construction. If a ternary expression isn't robust enough, you can use if statements outside of your JSX to determine which components should be used. Or if you prefer a more "inline" aesthetic, define immediately-invoked function expressions inside your JSX.

Another way of writing the same code:

render(){
    return(
        <div>
            <div className="form-group">
                ....   
            </div>
            {this._callFun()}    
        </div>
    )
}

_callFun(){
    if (this.props.status === 'PENDING') {
        return (<div className="loading" />);
    }
}
like image 86
Mayank Shukla Avatar answered Sep 23 '22 11:09

Mayank Shukla