is legitimate because div is a valid HTML tag:
+ return
Hello {props.toWhat}
;
+}
+
+function HelloWorld() {
+ // Correct! React knows
is a component because it's capitalized.
+ return
;
+}
+```
+
+### Choosing the Type at Runtime
+
You cannot use a general expression as the React element type. If you do want to use a general expression to indicate the type of the element, just assign it to a capitalized variable first. This often comes up when you want to render a different component based on a prop:
-```js
+```js{10,11}
import React from 'react';
import { PhotoStory, VideoStory } from './stories';
const components = {
- photo:
,
- video:
,
+ photo: PhotoStory,
+ video: VideoStory
};
-function Story1(props) {
- // Not valid JSX
- return
;
+function Story(props) {
+ // Wrong! JSX type can't be an expression.
+ return
;
}
+```
-function render2(props) {
- const MyComponent = components[props.story];
+To fix this, we will assign the type to a capitalized variable first:
- // Valid JSX
- return
;
+```js{9-11}
+import React from 'react';
+import { PhotoStory, VideoStory } from './stories';
+
+const components = {
+ photo: PhotoStory,
+ video: VideoStory
+};
+
+function Story(props) {
+ // Correct! JSX type can be a capitalized variable.
+ const SpecificStory = components[props.storyType];
+ return
;
}
```
@@ -134,7 +182,7 @@ For `MyComponent`, The value of `props.foo` will be `10` because the expression
`if` statements and `for` loops are not expressions in JavaScript, so they can't be used in JSX directly. Instead, you can put these in the surrounding code. For example:
-```js
+```js{3-7}
function NumberDescriber(props) {
let description;
if (props.number % 2 == 0) {
@@ -153,7 +201,7 @@ You can pass a string literal as a prop. These two JSX expressions are equivalen
```js
-
+
```
When you pass a string literal, its value is HTML-unescaped. So these two JSX expressions are equivalent:
@@ -161,7 +209,7 @@ When you pass a string literal, its value is HTML-unescaped. So these two JSX ex
```js
-
+
```
This behavior is usually not relevant. It's only mentioned here for completeness.
@@ -176,20 +224,20 @@ If you pass no value for a prop, it defaults to `true`. These two JSX expression
```
-In general, we don't recommend using this because it can be confused with the ES6 object shorthand `{foo}` which is short for {foo: foo} rather than `{foo: true}`. This behavior is just there so that it matches the behavior of HTML.
+In general, we don't recommend using this because it can be confused with the [ES6 object shorthand](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Object_initializer#New_notations_in_ECMAScript_2015) `{foo}` which is short for `{foo: foo}` rather than `{foo: true}`. This behavior is just there so that it matches the behavior of HTML.
### Spread Attributes
-If you already have `props` as an object, and you want to pass it in JSX, you can use `...` as a "spread" operator to pass the whole props object. These two render functions are equivalent:
+If you already have `props` as an object, and you want to pass it in JSX, you can use `...` as a "spread" operator to pass the whole props object. These two components are equivalent:
-```js
-function render1() {
- const props = {left: 'ben', right: 'hector'};
- return
;
+```js{7}
+function App1() {
+ return
;
}
-function render2() {
- return
;
+function App2() {
+ const props = {firstName: 'Ben', lastName: 'Hector'};
+ return
;
}
```
@@ -270,12 +318,12 @@ You can pass any JavaScript expression as children, by enclosing it within `{}`.
This is often useful for rendering a list of JSX expressions of arbitrary length. For example, this renders an HTML list:
-```js
+```js{2,9}
function Item(props) {
return
{props.message};
}
-function renderTodoList() {
+function TodoList() {
const todos = ['finish doc', 'submit pr', 'nag dan to review'];
return (
@@ -287,15 +335,17 @@ function renderTodoList() {
JavaScript expressions can be mixed with other types of children. This is often useful in lieu of string templates:
-```js
+```js{2}
function Hello(props) {
return Hello {props.addressee}!
;
}
```
+### Functions as Children
+
Normally, JavaScript expressions inserted in JSX will evaluate to a string, a React element, or a list of those things. However, `props.children` works just like any other prop in that it can pass any sort of data, not just the sorts that React knows how to render. For example, if you have a custom component, you could have it take a callback as `props.children`:
-```js
+```js{4,13}
function ListOfTenThings() {
return (
@@ -307,16 +357,18 @@ function ListOfTenThings() {
// Calls the children callback numTimes to produce a repeated component
function Repeat(props) {
let items = [];
- for (let i = 0; i < numTimes; i++) {
+ for (let i = 0; i < props.numTimes; i++) {
items.push(props.children(i));
}
- return {items}
+ return {items}
;
}
```
Children passed to a custom component can be anything, as long as that component transforms them into something React can understand before rendering. This usage is not common, but it works if you want to stretch what JSX is capable of.
-`false`, `null`, 'undefined', and 'true' are valid children. They simply don't render. These JSX expressions will all render to the same thing:
+### Booleans, Null, and Undefined Are Ignored
+
+`false`, `null`, `undefined`, and `true` are valid children. They simply don't render. These JSX expressions will all render to the same thing:
```js
@@ -330,8 +382,39 @@ Children passed to a custom component can be anything, as long as that component
{true}
```
-This can be useful to conditionally render React elements. This JSX only renders a `` if `showHeader` is true:
+This can be useful to conditionally render React elements. This JSX only renders a `` if `showHeader` is `true`:
-```js
-{showHeader && }
+```js{2}
+
+ {showHeader && }
+
+
+```
+
+One caveat is that some ["falsy" values](https://developer.mozilla.org/en-US/docs/Glossary/Falsy), such as the `0` number, are still rendered by React. For example, this code will not behave as you might expect because `0` will be printed when `props.messages` is an empty array:
+
+```js{2}
+
+ {props.messages.length &&
+
+ }
+
+```
+
+To fix this, make sure that the expression before `&&` is always boolean:
+
+```js{2}
+
+ {props.messages.length > 0 &&
+
+ }
+
+```
+
+Conversely, if you want a value like `false`, `true`, `null`, or `undefined` to appear in the output, you have to [convert it to a string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String#String_conversion) first:
+
+```js{2}
+
+ My JavaScript variable is {String(myVariable)}.
+
```
diff --git a/docs/refs-and-the-dom.md b/docs/refs-and-the-dom.md
index e49766de..9ca5aedc 100644
--- a/docs/refs-and-the-dom.md
+++ b/docs/refs-and-the-dom.md
@@ -15,9 +15,9 @@ In the typical React dataflow, [props](/react/docs/components-and-props.html) ar
React supports a special attribute that you can attach to any component. The `ref` attribute takes a callback function, and the callback will be executed immediately after the component is mounted or unmounted.
-When the `ref` attribute is used on a HTML element, the `ref` callback receives the underlying DOM element as its argument. For example, this code uses the `ref` callback to store a reference to a DOM node:
+When the `ref` attribute is used on an HTML element, the `ref` callback receives the underlying DOM element as its argument. For example, this code uses the `ref` callback to store a reference to a DOM node:
-```javascript
+```javascript{8,9,19}
class CustomTextInput extends React.Component {
constructor(props) {
super(props);
@@ -31,10 +31,12 @@ class CustomTextInput extends React.Component {
render() {
// Use the `ref` callback to store a reference to the text input DOM
- // element in this.textInput
+ // element in this.textInput.
return (