Pass state to child components as props
Define which components own state
Use stateless functional components
State is always passed from a parent down to a child component as a prop
State should not be passed to a sibling or a parent
class App extends Component {
render() {
return (
<div>
<Navbar />
<TicTacToe />
</div>
);
}
}
class InstructorItem extends Component {
constructor(props) {
super(props);
this.state = {
name: this.props.name,
hobbies: this.props.hobbies
};
}
render() {
return (
<div>
<h3>{this.state.name}</h3>
<h4>
Hobbies: {this.state.hobbies.join(", ")}
</h4>
</div>
);
}
}
Bad Practice: Don't Do This
Stateless Functional Components
import React from 'react';
const Greeting = props => (
<h1>Hello, {props.name}</h1>
);
export default Greeting;
Stateless Functional Component
Use a function as the first parameter to setState
Add a callback to setState to determine when the state is up to date
this.state = { counter: 1 };
this.setState({
counter: this.state.counter + 1
});
this.setState({
counter: this.state.counter + 1
});
Object.assign({},
{counter: this.state.counter + 1},
{counter: this.state.counter + 1},
{counter: this.state.counter + 1},
);
this.setState({
counter: this.state.counter + 1
});
this.setState((prevState, props) => {
return {
counter: prevState.counter + 1
};
});
this.setState({name: "Tim"});
// Won't be updated yet
console.log(this.state.name);
this.setState({name: "Tim"}, () => {
console.log(
"Now state is up to date",
this.state.name
);
});
Describe the virtual DOM
Define a synthetic events
Describe changes in React 16 (Fiber)
Instructor
Instructor
Instructor
Instructor
Instructor
render() {
return [
<div key='a'>First Element</div>,
<div key='b'>Second Element</div>
];
}
Render can return an array of JSX elements or a string
Demonstrate an onClick event
Using bond functions vs inline callbacks
class ClickExample extends Component {
constructor(props) {
super(props);
this.state = { name: "tim" };
}
render() {
return (
<div>
<p>{this.state.name}</p>
<button type="button"
onClick={() => this.setState({name: "TIM"})}>
UPPERCASE
</button>
</div>
);
}
}
class ClickExample extends Component {
constructor(props) {
super(props);
this.state = { name: "tim" };
this.handleClick = this.handleClick.bind(this);
}
handleClick(e) {
this.setState((prevState, props) => ({
name: prevState.name.toUpperCase()
});
}
render() {
return (
<div>
<p>{this.state.name}</p>
<button type="button" onClick={this.handleClick}>
UPPERCASE
</button>
</div>
);
}
}
render() {
return (
<div>
<p>{this.state.name}</p>
<button type="button" onClick={this.handleClick()}>
UPPERCASE
</button>
</div>
);
}
The function will be invoked immediatley, not on the event
Describe a controlled component vs uncontrolled
Handle a submit using onSubmit
<input type="text" />
React is not aware of what the user is typing, the browser is in charge of the state
<input type="text" value={this.state.inputText}/>
React is now in control of the state via this.state.inputText, but the input can't be updated
<input
type="text"
name="inputText"
value={this.state.inputText}
onChange={(e) => {
this.setState({inputText: e.target.value})
}}
/>
React is now in control of the state via this.state.inputText and the state can change via onChange
<form onSubmit={(e) => {
e.preventDefault();
const data = [...this.state.data,
this.state.inputText];
this.setState({data, inputText: ''});
}}>
<input
type="text"
name="inputText"
value={this.state.inputText}
onChange={(e) => {
this.setState({[e.target.name]: e.target.value})
}}
/>
</form>
<form>
<input
type="text"
name="inputText"
value={this.state.inputText}
onChange={(e) => {
this.setState({[e.target.name]: e.target.value})
}}
/>
<button
onClick={ /* trying to handle submit here */ }
type="submit"
>
SAVE
</button>
</form>
Define a ref in react
Use a ref on an uncontrolled input component
Managing focus, text selection, or media playback
Triggering imperative animations
Integrating with third-party DOM libraries
<form onSubmit={(e) => {
e.preventDefault();
// access to the form value:
console.log(this.inputText.value);
}}>
<input
type="text"
ref={(input) => this.inputText = input}
/>
</form>
const CardState = {
HIDING: 0,
SHOWING: 1,
MATCHING: 2
};
let cards = [
{id: 0, cardState: CardState.HIDING, backgroundColor: 'red'},
{id: 1, cardState: CardState.HIDING, backgroundColor: 'red'},
{id: 2, cardState: CardState.HIDING, backgroundColor: 'navy'},
{id: 3, cardState: CardState.HIDING, backgroundColor: 'navy'},
{id: 4, cardState: CardState.HIDING, backgroundColor: 'green'},
{id: 5, cardState: CardState.HIDING, backgroundColor: 'green'},
{id: 6, cardState: CardState.HIDING, backgroundColor: 'yellow'},
{id: 7, cardState: CardState.HIDING, backgroundColor: 'yellow'},
{id: 8, cardState: CardState.HIDING, backgroundColor: 'black'},
{id: 9, cardState: CardState.HIDING, backgroundColor: 'black'},
{id: 10, cardState: CardState.HIDING, backgroundColor: 'purple'},
{id: 11, cardState: CardState.HIDING, backgroundColor: 'purple'},
{id: 12, cardState: CardState.HIDING, backgroundColor: 'pink'},
{id: 13, cardState: CardState.HIDING, backgroundColor: 'pink'},
{id: 14, cardState: CardState.HIDING, backgroundColor: 'lightskyblue'},
{id: 15, cardState: CardState.HIDING, backgroundColor: 'lightskyblue'}
];
this.state = {cards: shuffle(cards)};
Describe the component lifecycle methods
Describe use cases for lifecycle methods
const NUM_BOXES = 32;
class Boxes extends Component {
constructor(props) {
super(props);
const boxes = Array(NUM_BOXES).fill()
.map(this.getRandomColor, this);
this.state = {boxes};
this.intervalId = setInterval(() => {
const boxes = this.state.boxes.slice();
const ind = Math.floor(Math.random()*boxes.length);
boxes[ind] = this.getRandomColor();
this.setState({boxes});
}, 300);
}
componentWillUnmount() {
clearInterval(this.intervalId);
}
}
GET https://hacker-news.firebaseio.com/v0/topstories.json
[15300069,15298833,...15267532]
GET https://hacker-news.firebaseio.com/v0/item/15300069.json
{"by":"maguay", "id":15300069, "title":"Google signs agreement with HTC", "url":"https://www.blog.google/topics/hardware/google-signs-agreement-htc-continuing-our-big-bet-hardware/" }
[ ...,
{"name":"United States of America",
"population":323947000, "latlng":[38.0,-97.0],
"flag":"https://restcountries.eu/data/usa.svg"},
{"name":"Mexico",
"population":122273473,"latlng":[23.0,-102.0],
"flag":"https://restcountries.eu/data/mex.svg"},
...
]
GET https://restcountries.eu/rest/v2/all