Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why if-else condition is not working while using in react jsx

writing if-else in jsx while writing react code is not working.

<div id={if (condition) { 'msg' }}>Hello World!</div>

However using ternary operator works.

<div id={condition ? 'msg' : null}>Hello World!</div>

why is this happening?

like image 271
crystalthinker Avatar asked Aug 23 '16 10:08

crystalthinker


People also ask

Can I write if-else condition in JSX?

if-else statements don't work inside JSX. This is because JSX is just syntactic sugar for function calls and object construction.

Does if-else work in React?

if/else. Conditional rendering in React works the same way conditions work in JavaScript. Use JavaScript operators like if, and let React update the UI to match them. We use an if with our condition and return the element to be rendered.

Is JSX can be used inside if and for loop?

Yes you an create a for loop inside react JSX with the help of function callback approach.

How do I fix JSX expressions must have one parent element?

js error "JSX expressions must have one parent element" occurs when a component returns multiple elements. To solve the error, wrap the elements in a parent div element or use a fragment, e.g. <><h2>One</h2><h2>Two</h2></> .


3 Answers

Your JSX of

<div id={condition ? 'msg' : null}>Hello World!</div>

which is not valid Javascript by itself, will be compiled into the following ReactJS call:

React.createElement(
  'div',                            // Element "tag" name.
  { id: condition ? 'msg' : null }, // Properties object.
  'Hello World!'                    // Element contents.
);

which is valid Javascript, ready to be interpreted/compiled by your Javascript runtime environment. As you can see, there is no way to jam an if-else into that statement, as it cannot be compiled into valid Javascript.


You could instead use an immediately-invoked function expression and pass the value returned from within:

<div id={(function () {
    if (condition) {
        return "msg";
    } else {
        return null;
    }
})()}>Hello World!</div>

which will compile into the following valid Javascript:

React.createElement(
    "div",
    {
        id: (function () {
            if (condition) {
                return "msg";
            } else {
                return null;
            }
        })()
    },
    "Hello World!"
);
like image 98
John Weisz Avatar answered Nov 14 '22 23:11

John Weisz


if-else statements don't work inside JSX. This is because JSX is just syntactic sugar for function calls and object construction. React Docs

like image 21
Anuj Verma Avatar answered Nov 15 '22 01:11

Anuj Verma


// This JSX:
<div id={if (condition) { 'msg' }}>Hello World!</div>

// Is transformed to this JS:
React.createElement("div", {id: if (condition) { 'msg' }}, "Hello World!");

So, you see if/else does not fit in this model. Better to use it outside of jsx. may be in render function.

like image 22
Mohit Verma Avatar answered Nov 15 '22 01:11

Mohit Verma