React.createElement(component, props, ...children)
<Foo />
vs <div>
(Incorrect
)
function Story(props) {
// Wrong! JSX type can't be an expression.
return <components[props.storyType] story={props.story} />;
};
(Corrected
)
function Story(props) {
// Correct! JSX type can be a capitalized variable.
const SpecificStory = components[props.storyType];
return <SpecificStory story={props.story} />;
}
Props in JSX
Javascript Expressions as Props
String Literals
Props Default to “True”
Spread Attributes
Children in JSX
props.children
: The content between opening and closing tag.JavaScript Expressions as Children
function Item(props) {
return <li>{props.message}</li>;
}
function TodoList() {
const todos = ["finish doc", "submit pr", "nag dan to review"];
return (
<ul>
{todos.map((message) => (
<Item key={message} message={message} />
))}
</ul>
);
}
Functions as Children
props.children
works like any other prop, meaning it can pass any sort of data.// Calls the children callback numTimes to produce a repeated component
function Repeat(props) {
let items = [];
for (let i = 0; i < props.numTimes; i++) {
items.push(props.children(i));
}
return <div>{items}</div>;
}
function ListOfTenThings() {
return (
<Repeat numTimes={10}>
{(index) => <div key={index}>This is item {index} in the list</div>}
</Repeat>
);
}
Booleans, Null, and Undefined Are Ignored
false
, null
, undefined
, and true
are all valid children.
You can use these to conditionally render items.
showHeader
evals to True.// Before work-around
<div>
{props.messages.length &&
<MessageList messages={props.messages} />
}
</div>
// After work-around
<div>
{props.messages.length > 0 &&
<MessageList messages={props.messages} />
}
</div>
Note that certain falsy values such as zero will still be rendered by React, you can work around this by ensuring situations like the above eval. into a boolean.
In the times you want booleans to be rendered out, simply convert it into a string first.
The Diffing Algorithm
Diffing
: When the state of a component changes React creates a new virtual DOM tree.
Elements of Different Types
DOM Elements Of the Same Type
Component Elements Of The Same Type
Recursing On Children
keys
.
typechecking
to catch bugs.propTypes
is a special property to run typechecking.
import PropTypes from "prop-types";
class Greeting extends React.Component {
render() {
return <h1>Hello, {this.props.name}</h1>;
}
}
Greeting.propTypes = {
name: PropTypes.string,
};
Requiring Single Child
PropTypes.element
to specify only a single child can be passed to a component as children.import PropTypes from "prop-types";
class MyComponent extends React.Component {
render() {
// This must be exactly one element or it will warn.
const children = this.props.children;
return <div>{children}</div>;
}
}
MyComponent.propTypes = {
children: PropTypes.element.isRequired,
};
Default Prop Values
defaultProps
to assign default values for props.class Greeting extends React.Component {
render() {
return <h1>Hello, {this.props.name}</h1>;
}
}
// Specifies the default values for props:
Greeting.defaultProps = {
name: "Stranger",
};
// Renders "Hello, Stranger":
ReactDOM.render(<Greeting />, document.getElementById("example"));
class Greeting extends React.Component {
static defaultProps = {
name: 'stranger'
}
render() {
return (
<div>Hello, {this.props.name}</div>
)
}