How to do the comparison using if inside the render function

gates

I am trying to load a component either Two or Three with the help of the state I obtain from the input. I know how to use the ternary operator inside the render function. It's like this

render: function(){
 return( <div>
         {this.state.in===2?<Two/>: <Three/>}
         </div>
          )}


But this would work only for two comparisons, If I have ten components and wants to load 10 different components on 10 different choices. I have go for if. Here is my try regarding it. I am unable to keep if conditions inside {} like I did with ternary operator and If I don't keep them inside {}, render is loading it as normal text. Here is my code

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>Hello React</title>
    <script src="https://fb.me/react-0.13.3.js"></script>
    <script src="https://fb.me/JSXTransformer-0.13.3.js"></script>
  </head>
  <body>
    <div id="example"></div>
    <script type="text/jsx">

     var One = React.createClass({

      getInitialState: function(){
        return {in:2}
      },

      handler: function(eve){

        this.setState({
           in: eve.target.value
        })
      },

      render: function(){
        return(
          <div>
          <input value={this.state.in} onChange={this.handler} />
           if(this.state.in ===2){
              <Two/>
            }
            if(this.state.in ===3){
              <Three />
            }

          </div>
          ) 

      }

     }); 


     var Two = React.createClass({

      render: function(){
        return(<div>
            This is component 2
          </div>)

      }

     });

     var Three = React.createClass({

        render: function(){
          return(<div>This is the third one</div>)

        }


     })


      React.render(<One/>,document.getElementById('example'));

    </script>
  </body>
</html>

ps: For further reading and official docs, take a look at this http://facebook.github.io/react/tips/if-else-in-JSX.html

Carla França

Maybe something like this:

render: function(){
        //Get all your component names and create an array
        var components = ["One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten"];

        return(
          <div>
              <input value={this.state.in} onChange={this.handler} />
              < components[this.state.in - 1] />
            </div>
          );

      }

});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How to Call a Function inside a Render in React/Jsx

From Dev

How do you access a function inside an anonymous function from outside

From Dev

How do I call function inside another function?

From Dev

How do I call a function inside a function

From Dev

How do I make a referential type comparison in Swift using 'is'?

From Dev

How do render data as indented list in html using jquery?

From Dev

How do I render a velocity template inside a issue tab panel?

From Dev

How do I call a function inside another function in VBA?

From Dev

How to do the comparison using if inside the render function

From Dev

How do I call a parent function inside a nested function in lodash?

From Dev

How do I render a function which contain a view in react native?

From Dev

How to add comment inside the return statement in render function?

From Dev

IF block inside render function ReactJS

From Dev

How to use an ejs variable inside a react render function?

From Dev

How to render partial inside template

From Dev

Using jQuery inside of a React render function

From Dev

How do you connect to database inside a function

From Dev

How to render input inside label - using zend form

From Dev

How do I place a for loop inside a function?

From Dev

How do I use orthogonal data in DataTables to sort when using HTML in a render function?

From Dev

How do i include and overloading function inside an overload function

From Dev

Performance of function expression inside render function

From Dev

how to only render one view inside of the epoxy render function

From Dev

How do I put a function inside a macro?

From Dev

How do I render a partial containing a form_for for an edit function?

From Dev

How to render a JSX that is inside a variable

From Dev

how to append drop down options inside render function?

From Dev

how to call function inside reactjs render method properly?

From Dev

How to render components using function in React Native

Related Related

  1. 1

    How to Call a Function inside a Render in React/Jsx

  2. 2

    How do you access a function inside an anonymous function from outside

  3. 3

    How do I call function inside another function?

  4. 4

    How do I call a function inside a function

  5. 5

    How do I make a referential type comparison in Swift using 'is'?

  6. 6

    How do render data as indented list in html using jquery?

  7. 7

    How do I render a velocity template inside a issue tab panel?

  8. 8

    How do I call a function inside another function in VBA?

  9. 9

    How to do the comparison using if inside the render function

  10. 10

    How do I call a parent function inside a nested function in lodash?

  11. 11

    How do I render a function which contain a view in react native?

  12. 12

    How to add comment inside the return statement in render function?

  13. 13

    IF block inside render function ReactJS

  14. 14

    How to use an ejs variable inside a react render function?

  15. 15

    How to render partial inside template

  16. 16

    Using jQuery inside of a React render function

  17. 17

    How do you connect to database inside a function

  18. 18

    How to render input inside label - using zend form

  19. 19

    How do I place a for loop inside a function?

  20. 20

    How do I use orthogonal data in DataTables to sort when using HTML in a render function?

  21. 21

    How do i include and overloading function inside an overload function

  22. 22

    Performance of function expression inside render function

  23. 23

    how to only render one view inside of the epoxy render function

  24. 24

    How do I put a function inside a macro?

  25. 25

    How do I render a partial containing a form_for for an edit function?

  26. 26

    How to render a JSX that is inside a variable

  27. 27

    how to append drop down options inside render function?

  28. 28

    how to call function inside reactjs render method properly?

  29. 29

    How to render components using function in React Native

HotTag

Archive