You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: content/docs/conditional-rendering.md
+28-28
Original file line number
Diff line number
Diff line change
@@ -8,11 +8,11 @@ redirect_from:
8
8
- "tips/false-in-jsx.html"
9
9
---
10
10
11
-
In React, you can create distinct components that encapsulate behavior you need. Then, you can render only some of them, depending on the state of your application.
Conditional rendering in React works the same way conditions work in JavaScript. Use JavaScript operators like [`if`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if...else)or the [conditional operator](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Conditional_Operator) to create elements representing the current state, and let React update the UI to match them.
It will render either `<LoginButton />`or`<LogoutButton />` depending on its current state. It will also render a `<Greeting />` from the previous example:
While declaring a variable and using an `if`statement is a fine way to conditionally render a component, sometimes you might want to use a shorter syntax. There are a few ways to inline conditions in JSX, explained below.
You may [embed any expressions in JSX](/docs/introducing-jsx.html#embedding-expressions-in-jsx) by wrapping them in curly braces. This includes the JavaScript logical `&&` operator. It can be handy for conditionally including an element:
Another method for conditionally rendering elements inline is to use the JavaScript conditional operator [`condition ? true : false`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Conditional_Operator).
In the example below, we use it to conditionally render a small block of text.
159
+
在下面这个示例中,我们用它来条件渲染一小段文本
160
160
161
161
```javascript{5}
162
162
render() {
@@ -169,7 +169,7 @@ render() {
169
169
}
170
170
```
171
171
172
-
It can also be used for larger expressions although it is less obvious what's going on:
172
+
同样的,它也可以用于较为复杂的表达式中,虽然看起来不是很直观:
173
173
174
174
```js{5,7,9}
175
175
render() {
@@ -186,13 +186,13 @@ render() {
186
186
}
187
187
```
188
188
189
-
Just like in JavaScript, it is up to you to choose an appropriate style based on what you and your team consider more readable. Also remember that whenever conditions become too complex, it might be a good time to [extract a component](/docs/components-and-props.html#extracting-components).
In rare cases you might want a component to hide itself even though it was rendered by another component. To do this return `null` instead of its render output.
In the example below, the `<WarningBanner />`is rendered depending on the value of the prop called`warn`. If the value of the prop is `false`, then the component does not render:
Returning `null` from a component's `render` method does not affect the firing of the component's lifecycle methods. For instance `componentDidUpdate`will still be called.
0 commit comments