Browse Source

Add example to if else in JSX.

main
Sunny Juneja 11 years ago
parent
commit
8f5a2e564d
  1. 21
      tips/03-if-else-in-JSX.md

21
tips/03-if-else-in-JSX.md

@ -39,4 +39,25 @@ That's not valid JS. You probably want to make use of a ternary expression:
React.renderComponent(<div id={condition ? 'msg' : ''}>Hello World!</div>, mountNode);
```
If a ternary expression isn't robust enough, you can use `if` statements to determine which
components should be used.
```js
/** @jsx React.DOM */
var loginButton;
if (loggedIn) {
loginButton = <LogoutButton />;
} else {
loginButton = <LoginButton />;
}
return (
<nav>
<Home />
{loginButton}
</nav>
)
```
Try using it today with the [JSX compiler](/react/jsx-compiler.html).

Loading…
Cancel
Save