2

function Demo(props){


    return(
        <div>
            {props.boolean && <div>}  
            <h1>
                HI,many of these kind of dom elements will be here
             </h1>
            {props.boolean && </div>}
        </div>

    )

}

ReactDOM.render(
  <Demo boolean="true"/>, document.body
);
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<!DOCTYPE html>

</head>
<body>

</body>
</html>

Here i want to get the form tag if the prop boolean value is true,but its not working as expected. if i use code like this

   {props.boolean && <div></div>}  
    <h1>
        HI,many of these kind of dom elements will be here
     </h1>
    {props.boolean && <div></div>}

it comes,So if opening and closing tags are together then its working.is there any way to get the values when the tags are apart...please help

6
  • {props.boolean && <div></div>} , what you expect to do with this condition? Commented Jul 25, 2017 at 12:52
  • just use a normal if there. Commented Jul 25, 2017 at 12:54
  • What are you trying to do exactly? Why are you using HTML tags to test for truth? Commented Jul 25, 2017 at 12:55
  • i have some other scenarious where i have to include set of codes in some DOM element if the condition meets... Commented Jul 25, 2017 at 12:59
  • @Álvaro Touzón, i just wrote to show it works if tags are ended like that Commented Jul 25, 2017 at 13:00

3 Answers 3

1

You're trying to do something, that's somewhat of an anti-pattern. This might be a better solution:

function Demo(props) {
    function MyContents() {
        return (
            <h1>
                HI,many of these kind of dom elements will be here
            </h1>
        );
    }

    return (
        <div>
            { props.boolean ?
                <div><MyContents /></div>
                :
                <MyContents />
            };
        </div>
    );
}

ReactDOM.render(
    <Demo boolean="true" />, document.body
);
Sign up to request clarification or add additional context in comments.

Comments

0

Please try Conditional Rendering

function Demo(props) {
    const flag = props.flag;
    return (
        if (flag) {
            return <h1 > true < /h1>;
        }

        return <h1 > false < /h1>;

    )
}

 ReactDOM.render(
  <Demo flag={true}/>,  document.getElementById('root')
);

HTML

<body>
  <div id="root"></div>
</body>

Comments

0

A different and better way to do conditional rendering

function Demo(props) {
    return (
      <h1>{props.flag ? true : false}</h1>;
    )
}

 ReactDOM.render(
  <Demo flag={true}/>,  document.getElementById('root')
);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.