import React from 'react';
const Item = ({id, name}) => <li>{id} {name}</li>;
const List = ({items}) => <ul>{items.map(Item)}</ul>;import {render} from 'react-dom';
const items = [
{id:1, name: 'test'},
{id:2, name: 'test'}
];
// assuming there is an element with id 'app' in the DOM
render(<List items={items}/>, document.getElementById('app'));They are props renderers
import React from 'react';
const Item = ({id, name}) => <li>{id} {name}</li>;
const List = ({items}) => <ul>{items.map(Item)}</ul>;class App extends ReactComponent {
state = {
items: [ /* ... */ ]
};
render() {
return (
<List items={this.state.items}/>
);
}
}
import React from 'react';
import {render} from 'react-dom';
class App extends React.Component {
componentWillMount() { /* e.g.: update the state from initial props */ }
componentDidMount() { /* e.g.: bind an external UI component */ }
render() { /* ... */ }
};import React from 'react';
import {render} from 'react-dom';
class App extends React.Component {
componentWillReceiveProps(nextProps) { /* e.g.: update the state from new props */ }
shouldComponentUpdate(nextProps, nextState) { /* e.g.: shallow comparison */ }
componentWillUpdate(nextProps, nextState) { /* e.g.: make some computations */ }
render() { /* ... */ }
componentDidUpdate(prevProps, prevState) { /* e.g.: update UI bindings */ }
};import React from 'react';
import {render} from 'react-dom';
class App extends React.Component {
componentWillUnmount() { /* e.g.: unbind external UI widgets */ }
};
→ components do not manipulate their props
This code won't update the input value while typing.
import React from 'react';
import {render} from 'react-dom';
const MyInput = ({value, onChange}) => (
<input
value={value}
onChange={(event) => onChange(event.target.value)}
/>
);
render(<MyInput value={'test'} onChange={() => {}} />, document.getElementById('app'));This code will update the input value while typing.
import React from 'react';
import {render} from 'react-dom';
const MyInput = ({value, onChange}) => (
<input
value={value}
onChange={(event) => onChange(event.target.value)}
/>
);
class App extends React.Component {
state = { value: 'test' };
render() {
return (
<MyInput
value={this.state.value}
onChange={(value) => this.setState({value})}
/>
);
}
};
render(<App />, document.getElementById('app'));This is a mutation.
This is not a mutation.
let list = [0, 1, 2];
list.push(3);
let nextList = list;let list = [0, 1, 2];
let nextList = [...list, 3];To know if the array has changed, we should compare each item
let list = [0, 1, 2];
list.push(3);
let nextList = list;To know if the array has changed, we compare references
let list = [0, 1, 2];
let nextList = [...list, 3];To know if the props or state has changed, we do a shallow comparison
import React from 'react';
import {render} from 'react-dom';
import shallowCompare from 'react-addons-shallow-compare';
class App extends React.Component {
shouldComponentUpdate(nextProps, nextState) {
return shallowCompare(this, nextProps, nextState);
}
};Why a todo list again ?
Features:
Questions to ask yourself before starting: