Professional Documents
Culture Documents
let const
https://reactjs.org/tutorial/tutorial.html 1/56
12/27/2017 Tutorial: Intro To React - React
src/
cd my-app
rm -f src/*
https://reactjs.org/tutorial/tutorial.html 2/56
12/27/2017 Tutorial: Intro To React - React
index.css src/
index.js src/
index.js src/
React.Component
https://reactjs.org/tutorial/tutorial.html 3/56
12/27/2017 Tutorial: Intro To React - React
props
render
render
render
<div />
React.createElement('div')
https://reactjs.org/tutorial/tutorial.html 4/56
12/27/2017 Tutorial: Intro To React - React
createElement()
ShoppingList
<ShoppingList />
<button>
https://reactjs.org/tutorial/tutorial.html 5/56
12/27/2017 Tutorial: Intro To React - React
renderSquare value
https://reactjs.org/tutorial/tutorial.html 6/56
12/27/2017 Tutorial: Intro To React - React
render()
onClick onClick={alert('click')}
this.state
https://reactjs.org/tutorial/tutorial.html 7/56
12/27/2017 Tutorial: Intro To React - React
render() {
return (
<button className="square" onClick={() => alert('click')}>
{this.props.value}
</button>
);
}
}
super();
render
<button>
https://reactjs.org/tutorial/tutorial.html 8/56
12/27/2017 Tutorial: Intro To React - React
render() {
return (
<button className="square" onClick={() => this.setState({value: 'X'})}>
{this.state.value}
</button>
);
}
}
this.setState
this.state.value 'X'
https://reactjs.org/tutorial/tutorial.html 9/56
12/27/2017 Tutorial: Intro To React - React
https://reactjs.org/tutorial/tutorial.html 10/56
12/27/2017 Tutorial: Intro To React - React
renderSquare(i) {
return <Square value={i} />;
}
render() {
const status = 'Next player: X';
return (
<div>
<div className="status">{status}</div>
<div className="board-row">
{this.renderSquare(0)}
{this.renderSquare(1)}
{this.renderSquare(2)}
</div>
<div className="board-row">
{this.renderSquare(3)}
{this.renderSquare(4)}
{this.renderSquare(5)}
</div>
<div className="board-row">
{this.renderSquare(6)}
{this.renderSquare(7)}
{this.renderSquare(8)}
</div>
</div>
);
}
}
https://reactjs.org/tutorial/tutorial.html 11/56
12/27/2017 Tutorial: Intro To React - React
[
'O', null, 'X',
'X', 'X', 'O',
'O', null, null,
]
renderSquare
renderSquare(i) {
return <Square value={i} />;
}
value
renderSquare(i) {
return <Square value={this.state.squares[i]} />;
}
renderSquare
renderSquare(i) {
return (
<Square
value={this.state.squares[i]}
onClick={() => this.handleClick(i)}
/>
)
https://reactjs.org/tutorial/tutorial.html 12/56
12/27/2017 Tutorial: Intro To React - React
);
}
return
value onClick
constructor
onClick
onClick <button>
onClick
render()
this.props.onClick()
onClick {()
https://reactjs.org/tutorial/tutorial.html
> this handleClick(i)} 13/56
12/27/2017 Tutorial: Intro To React - React
onClick={() => this.handleClick(i)}
this.handleClick(i)
handleClick()
<button> onClick
onClick handleClick
on*
handle*
handleClick
handleClick(i) {
const squares = this.state.squares.slice();
squares[i] = 'X';
this.setState({squares: squares});
}
renderSquare(i) {
return (
<Square
value={this.state.squares[i]}
onClick={() => this.handleClick(i)}
/>
);
}
render() {
const status = 'Next player: X';
return (
<div>
<div className="status">{status}</div>
<div className="board-row">
{this.renderSquare(0)}
https://reactjs.org/tutorial/tutorial.html 14/56
12/27/2017 Tutorial: Intro To React - React
{this.renderSquare(1)}
{this.renderSquare(2)}
</div>
<div className="board-row">
{this.renderSquare(3)}
{this.renderSquare(4)}
{this.renderSquare(5)}
</div>
<div className="board-row">
{this.renderSquare(6)}
{this.renderSquare(7)}
{this.renderSquare(8)}
</div>
</div>
);
}
}
.slice() squares
.slice()
squares
https://reactjs.org/tutorial/tutorial.html 15/56
12/27/2017 Tutorial: Intro To React - React
// Or if you are using object spread syntax proposal, you can write:
// var newPlayer = {...player, score: 2};
https://reactjs.org/tutorial/tutorial.html 16/56
12/27/2017 Tutorial: Intro To React - React
shouldComponentUpdate()
render
React.Component
function Square(props) {
return (
<button className="square" onClick={props.onClick}>
{props.value}
</button>
);
}
this.props props
https://reactjs.org/tutorial/tutorial.html 17/56
12/27/2017 Tutorial: Intro To React - React
onClick={() =>
props.onClick()} onClick={props.onClick}
onClick={props.onClick()}
props.onClick
xIsNext
handleClick xIsNext
handleClick(i) {
const squares = this.state.squares.slice();
squares[i] = this.state.xIsNext ? 'X' : 'O';
this.setState({
squares: squares,
xIsNext: !this.state.xIsNext,
});
}
render
https://reactjs.org/tutorial/tutorial.html 18/56
12/27/2017 Tutorial: Intro To React - React
render() {
const status = 'Next player: ' + (this.state.xIsNext ? 'X' : 'O');
return (
// the rest has not changed
handleClick(i) {
const squares = this.state.squares.slice();
squares[i] = this.state.xIsNext ? 'X' : 'O';
this.setState({
squares: squares,
xIsNext: !this.state.xIsNext,
});
}
renderSquare(i) {
return (
<Square
value={this.state.squares[i]}
onClick={() => this.handleClick(i)}
/>
);
}
render() {
const status = 'Next player: ' + (this.state.xIsNext ? 'X' : 'O');
return (
<div>
<div className="status">{status}</div>
<div className="board-row">
{this.renderSquare(0)}
{this.renderSquare(1)}
https://reactjs.org/tutorial/tutorial.html 19/56
12/27/2017 Tutorial: Intro To React - React
{this.renderSquare(2)}
</div>
<div className="board-row">
{this.renderSquare(3)}
{this.renderSquare(4)}
{this.renderSquare(5)}
</div>
<div className="board-row">
{this.renderSquare(6)}
{this.renderSquare(7)}
{this.renderSquare(8)}
</div>
</div>
);
}
}
function calculateWinner(squares) {
const lines = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6],
];
for (let i = 0; i < lines.length; i++) {
const [a, b, c] = lines[i];
if (squares[a] && squares[a] === squares[b] && squares[a] === squares[c]) {
return squares[a];
}
}
return null;
}
https://reactjs.org/tutorial/tutorial.html 20/56
12/27/2017 Tutorial: Intro To React - React
render
status render
render() {
const winner = calculateWinner(this.state.squares);
let status;
if (winner) {
status = 'Winner: ' + winner;
} else {
status = 'Next player: ' + (this.state.xIsNext ? 'X' : 'O');
}
return (
// the rest has not changed
handleClick
handleClick(i) {
const squares = this.state.squares.slice();
if (calculateWinner(squares) || squares[i]) {
return;
}
squares[i] = this.state.xIsNext ? 'X' : 'O';
this.setState({
squares: squares,
xIsNext: !this.state.xIsNext,
});
}
https://reactjs.org/tutorial/tutorial.html 21/56
12/27/2017 Tutorial: Intro To React - React
squares
history = [
{
squares: [
null, null, null,
null, null, null,
null, null, null,
]
},
{
squares: [
null, null, null,
null, 'X', null,
null, null, null,
]
},
// ...
]
render() {
return (
<div className="game">
<div className="game-board">
<Board />
</div>
<div className="game-info">
<div>{/* status */}</div>
<ol>{/* TODO */}</ol>
</div>
</div>
);
}
}
squares onClick
constructor
this.state.squares[i] this.props.squares[i]
renderSquare
this.handleClick(i) this.props.onClick(i)
renderSquare
renderSquare(i) {
return (
<Square
value={this.props.squares[i]}
onClick={() => this.props.onClick(i)}
/>
);
}
render() {
const winner = calculateWinner(this.state.squares);
let status;
if (winner) {
status = 'Winner: ' + winner;
} else {
status = 'Next player: ' + (this.state.xIsNext ? 'X' : 'O');
}
return (
<div>
<div className="status">{status}</div>
<div className="board-row">
{this.renderSquare(0)}
{this.renderSquare(1)}
{this.renderSquare(2)}
</div>
<div className="board-row">
{this.renderSquare(3)}
{this.renderSquare(4)}
{this.renderSquare(5)}
</div>
<div className="board-row">
{this.renderSquare(6)}
{this.renderSquare(7)}
{this.renderSquare(8)}
</div>
</div>
);
}
}
render
https://reactjs.org/tutorial/tutorial.html 24/56
12/27/2017 Tutorial: Intro To React - React
render() {
const history = this.state.history;
const current = history[history.length - 1];
const winner = calculateWinner(current.squares);
let status;
if (winner) {
status = 'Winner: ' + winner;
} else {
status = 'Next player: ' + (this.state.xIsNext ? 'X' : 'O');
}
return (
<div className="game">
<div className="game-board">
<Board
squares={current.squares}
onClick={(i) => this.handleClick(i)}
/>
</div>
<div className="game-info">
<div>{status}</div>
<ol>{/* TODO */}</ol>
</div>
</div>
);
}
<div className="status">
{status}</div> render
render() {
return (
<div>
<div className="board-row">
{this.renderSquare(0)}
{this.renderSquare(1)}
{this.renderSquare(2)}
</div>
<div className="board-row">
{this.renderSquare(3)}
{this renderSquare(4)}
https://reactjs.org/tutorial/tutorial.html 25/56
12/27/2017 Tutorial: Intro To React - React
{this.renderSquare(4)}
{this.renderSquare(5)}
</div>
<div className="board-row">
{this.renderSquare(6)}
{this.renderSquare(7)}
{this.renderSquare(8)}
</div>
</div>
);
}
handleClick
handleClick
handleClick(i) {
const history = this.state.history;
const current = history[history.length - 1];
const squares = current.squares.slice();
if (calculateWinner(squares) || squares[i]) {
return;
}
squares[i] = this.state.xIsNext ? 'X' : 'O';
this.setState({
history: history.concat([{
squares: squares,
}]),
xIsNext: !this.state.xIsNext,
});
}
renderSquare render
https://reactjs.org/tutorial/tutorial.html 26/56
12/27/2017 Tutorial: Intro To React - React
render
render() {
const history = this.state.history;
const current = history[history.length - 1];
const winner = calculateWinner(current.squares);
let status;
if (winner) {
status = 'Winner: ' + winner;
} else {
status = 'Next player: ' + (this.state.xIsNext ? 'X' : 'O');
}
return (
<div className="game">
<div className="game-board">
<Board
squares={current.squares}
onClick={(i) => this.handleClick(i)}
/>
</div>
<div className="game-info">
<div>{status}</div>
<ol>{moves}</ol>
</div>
</div>
);
}
https://reactjs.org/tutorial/tutorial.html 27/56
12/27/2017 Tutorial: Intro To React - React
<li> <button>
https://reactjs.org/tutorial/tutorial.html 28/56
12/27/2017 Tutorial: Intro To React - React
alexa ben
claudia
key ref
key
this.props.key
key={i}
https://reactjs.org/tutorial/tutorial.html 29/56
12/27/2017 Tutorial: Intro To React - React
jumpTo
stepNumber: 0 constructor
jumpTo
xIsNext xIsNext
https://reactjs.org/tutorial/tutorial.html 30/56
12/27/2017 Tutorial: Intro To React - React
jumpTo
handleClick(i) {
// this method has not changed
}
jumpTo(step) {
this.setState({
stepNumber: step,
xIsNext: (step % 2) === 0,
});
}
render() {
// this method has not changed
}
stepNumber stepNumber:
history.length handleClick
handleClick stepNumber
handleClick(i) {
const history = this.state.history.slice(0, this.state.stepNumber + 1);
const current = history[history.length - 1];
const squares = current.squares.slice();
if (calculateWinner(squares) || squares[i]) {
return;
}
squares[i] = this.state.xIsNext ? 'X' : 'O';
this.setState({
history: history.concat([{
squares: squares
}]),
stepNumber: history.length,
xIsNext: !this.state.xIsNext,
});
}
render
https://reactjs.org/tutorial/tutorial.html 31/56
12/27/2017 Tutorial: Intro To React - React
render() {
const history = this.state.history;
const current = history[this.state.stepNumber];
const winner = calculateWinner(current.squares);
https://reactjs.org/tutorial/tutorial.html 32/56
12/27/2017 Tutorial: Intro To React - React
React.Component
https://reactjs.org/tutorial/tutorial.html 33/56
12/27/2017 Tutorial: Intro To React - React
https://reactjs.org/tutorial/tutorial.html 34/56
12/27/2017 Tutorial: Intro To React - React
https://reactjs.org/tutorial/tutorial.html 35/56
12/27/2017 Tutorial: Intro To React - React
https://reactjs.org/tutorial/tutorial.html 36/56
12/27/2017 Tutorial: Intro To React - React
https://reactjs.org/tutorial/tutorial.html 37/56
12/27/2017 Tutorial: Intro To React - React
https://reactjs.org/tutorial/tutorial.html 38/56
12/27/2017 Tutorial: Intro To React - React
https://reactjs.org/tutorial/tutorial.html 39/56
12/27/2017 Tutorial: Intro To React - React
https://reactjs.org/tutorial/tutorial.html 40/56
12/27/2017 Tutorial: Intro To React - React
https://reactjs.org/tutorial/tutorial.html 41/56
12/27/2017 Tutorial: Intro To React - React
https://reactjs.org/tutorial/tutorial.html 42/56
12/27/2017 Tutorial: Intro To React - React
https://reactjs.org/tutorial/tutorial.html 43/56
12/27/2017 Tutorial: Intro To React - React
https://reactjs.org/tutorial/tutorial.html 44/56
12/27/2017 Tutorial: Intro To React - React
https://reactjs.org/tutorial/tutorial.html 45/56
12/27/2017 Tutorial: Intro To React - React
https://reactjs.org/tutorial/tutorial.html 46/56
12/27/2017 Tutorial: Intro To React - React
https://reactjs.org/tutorial/tutorial.html 47/56
12/27/2017 Tutorial: Intro To React - React
https://reactjs.org/tutorial/tutorial.html 48/56
12/27/2017 Tutorial: Intro To React - React
https://reactjs.org/tutorial/tutorial.html 49/56
12/27/2017 Tutorial: Intro To React - React
https://reactjs.org/tutorial/tutorial.html 50/56
12/27/2017 Tutorial: Intro To React - React
https://reactjs.org/tutorial/tutorial.html 51/56
12/27/2017 Tutorial: Intro To React - React
https://reactjs.org/tutorial/tutorial.html 52/56
12/27/2017 Tutorial: Intro To React - React
https://reactjs.org/tutorial/tutorial.html 53/56
12/27/2017 Tutorial: Intro To React - React
https://reactjs.org/tutorial/tutorial.html 54/56
12/27/2017 Tutorial: Intro To React - React
https://reactjs.org/tutorial/tutorial.html 55/56
12/27/2017 Tutorial: Intro To React - React
https://reactjs.org/tutorial/tutorial.html 56/56