You are on page 1of 56

12/27/2017 Tutorial: Intro To React - React

let const

https://reactjs.org/tutorial/tutorial.html 1/56
12/27/2017 Tutorial: Intro To React - React

npm install -g create-react-app


create-react-app my-app

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/

import React from 'react';


import ReactDOM from 'react-dom';
import './index.css';

npm start http://localhost:3000

React.Component

https://reactjs.org/tutorial/tutorial.html 3/56
12/27/2017 Tutorial: Intro To React - React

class ShoppingList extends React.Component {


render() {
return (
<div className="shopping-list">
<h1>Shopping List for {this.props.name}</h1>
<ul>
<li>Instagram</li>
<li>WhatsApp</li>
<li>Oculus</li>
</ul>
</div>
);
}
}

// Example usage: <ShoppingList name="Mark" />

props
render

render
render

<div />
React.createElement('div')

return React.createElement('div', {className: 'shopping-list'},


React.createElement('h1', /* ... h1 children ... */),
React.createElement('ul', /* ... ul children ... */)
);

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

class Board extends React.Component {


renderSquare(i) {
return <Square value={i} />;
}

render {/* TODO */}


{this.props.value}

class Square extends React.Component {


render() {
return (
<button className="square">
{this.props.value}
</button>
);
}
}

https://reactjs.org/tutorial/tutorial.html 6/56
12/27/2017 Tutorial: Intro To React - React

render()

class Square extends React.Component {


render() {
return (
<button className="square" onClick={() => alert('click')}>
{this.props.value}
</button>
);
}
}

onClick onClick={alert('click')}

this.state

https://reactjs.org/tutorial/tutorial.html 7/56
12/27/2017 Tutorial: Intro To React - React

class Square extends React.Component {


constructor(props) {
super(props);
this.state = {
value: null,
};
}

render() {
return (
<button className="square" onClick={() => alert('click')}>
{this.props.value}
</button>
);
}
}

super();

render

this.props.value this.state.value <button>

() => alert() () => this.setState({value:


'X'})

<button>

class Square extends React.Component {


constructor(props) {
super(props);
this.state = {
value: null,
};
}

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

class Board extends React.Component {


constructor(props) {
super(props);
this.state = {
squares: Array(9).fill(null),
};
}

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

this.state.value this.props.value render

this.setState() this.props.onClick() render

constructor

class Square extends React.Component {


render() {
return (
<button className="square" onClick={() => this.props.onClick()}>
{this.props.value}
</button>
);
}
}

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

class Board extends React.Component {


constructor(props) {
super(props);
this.state = {
squares: Array(9).fill(null),
};
}

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

var player = {score: 1, name: 'Jeff'};


player.score = 2;
// Now player is {score: 2, name: 'Jeff'}

var player = {score: 1, name: 'Jeff'};

var newPlayer = Object.assign({}, player, {score: 2});


// Now player is unchanged, but newPlayer is {score: 2, name: 'Jeff'}

// 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

class Board extends React.Component {


constructor(props) {
super(props);
this.state = {
squares: Array(9).fill(null),
xIsNext: true,
};
}

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

class Board extends React.Component {


constructor(props) {
super(props);
this.state = {
squares: Array(9).fill(null),
xIsNext: true,
};
}

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,
]
},
// ...
]

class Game extends React.Component {


constructor(props) {
super(props);
this.state = {
history: [{
squares: Array(9).fill(null),
}],
xIsNext: true,
};
}
https://reactjs.org/tutorial/tutorial.html 22/56
12/27/2017 Tutorial: Intro To React - React

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

class Board extends React.Component {


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 23/56
12/27/2017 Tutorial: Intro To React - React

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);

const moves = history.map((step, move) => {


const desc = move ?
'Go to move #' + move :
'Go to game start';
return (
<li>
<button onClick={() => this.jumpTo(move)}>{desc}</button>
</li>
);
});

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>

<li>Alexa: 7 tasks left</li>


<li>Ben: 5 tasks left</li>

<li>Ben: 9 tasks left</li>


<li>Claudia: 8 tasks left</li>
<li>Alexa: 5 tasks left</li>

https://reactjs.org/tutorial/tutorial.html 28/56
12/27/2017 Tutorial: Intro To React - React

alexa ben
claudia

<li key={user.id}>{user.name}: {user.taskCount} tasks left</li>

key ref
key

this.props.key

key={i}

https://reactjs.org/tutorial/tutorial.html 29/56
12/27/2017 Tutorial: Intro To React - React

render <li key={move}>

const moves = history.map((step, move) => {


const desc = move ?
'Go to move #' + move :
'Go to game start';
return (
<li key={move}>
<button onClick={() => this.jumpTo(move)}>{desc}</button>
</li>
);
});

jumpTo

stepNumber: 0 constructor

class Game extends React.Component {


constructor(props) {
super(props);
this.state = {
history: [{
squares: Array(9).fill(null),
}],
stepNumber: 0,
xIsNext: true,
};
}

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);

// the rest has not changed

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

You might also like