Home > Net >  What is Immediate Invoke in React?
What is Immediate Invoke in React?

Time:07-14

What is this round-bracket thing inside the curly braces ?

  return (
    <div>
      {(() => (
        <h1>The Header</h1>
      ))()}
    </div>
  );
}
export default ImmediateInvoke;

CodePudding user response:

This is an IIFE and this is not a React feature. Just plain JavaScript.

CodePudding user response:

You declare a function and immediately call it. Useful in cases where you need to wrap a piece of code instead of writing this code in a global context, and automatically use it. In your case, an anonymous function is being created and called right after. If you wanted to save the function that you are calling, then declaration would need to happen first.

var myFunction = function() {
  console.log('Exextuing myFunction...');
};
myFunction();

This way you could also reuse it later if you wanted to.

  • Related