Immediately-invoked Function Expression Inside JSX
Solution 1:
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" />);
}
}
Post a Comment for "Immediately-invoked Function Expression Inside JSX"