You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
993 B
993 B
id | title | layout | permalink |
---|---|---|---|
if-else-in-JSX-tip | If-Else in JSX | docs | if-else-in-JSX-tip.html |
if-else
statements don't work inside JSX, since JSX is really just sugar for functions:
/** @jsx React.DOM */
// this
React.renderComponent(<div id="msg">Hello World!</div>, mountNode);
// is the same as this
React.renderComponent(React.DOM.div({id:"msg"}, "Hello World!"), mountNode);
Which means <div id={if (true){ 'msg' }}>Hello World!</div>
doesn't make sense, as (if it worked) it would be compiled down to something like this React.DOM.div({id: if (true){ 'msg' }}, "Hello World!")
, which isn't valid JS.
What you're searching for is ternary expression:
/** @jsx React.DOM */
// this
React.renderComponent(<div id={true ? 'msg' : ''}>Hello World!</div>, mountNode);
Try the JSX compiler to see how this works. It's a very simple transformation, thus making JSX entirely optional to use with React.