State is assigned using the class constructor
class Kitten extends Component {
constructor() {
super()
this.state = {
name: ''
};
...
}
render() {
return (
<div>
<h1>Meow, it's {this.state.name}'s Homepage</h1>
</div>
<div className="kitten">
<img src={kitten} alt="cute kitten" />
</div>
);
}
}Update state by passing an object containing part of the state to setState()
class Kitten extends React.Component {
...
setKittenName(name) {
this.setState({
name: name
});
}
componentWillMount() {
this.setKittenName('Greg');
}
render() {
...
}
}class Puppy extends Component {
constructor() {
super()
this.state = {
name: '',
treats: 0
};
...
}
twoTreats() {
this.setState({
treats: this.state.treats + 1
});
this.setState({
treats: this.state.treats + 1
});
}
...
render() {
return (
...
<h2>I want two treats!</h2>
<div>
<button onClick={this.twoTreats}>
Two treats please!
</button>
<div><h3>{this.state.treats}</h3></div>
</div>
...
);
}
}let puppy = { name: 'Fido' },
dog = { name: 'Fluffy' },
tiger = Object.assign({}, puppy, dog);
console.log(tiger); // { name: 'Fluffy' }When merging multiple objects with the same key, the value of the last object overrides the previous values
class App extends React.Component {
constructor() {
super()
this.state = {
name: '',
treats: 0
};
}
...
twoTreats() {
this.setState({
treats: this.state.treats + 1
});
this.setState({
treats: this.state.treats + 1
});
}
render() {
return (
<Puppies twoTreats={this.twoTreats}
treats={this.state.treats}
/>
)
}
}
}Refactor this.setState() to pass in a function that accepts the previous state and a callback function as a second parameter
twoTreats() {
this.setState((prevState, props) => ({
treats: prevState.treats +1
}));
this.setState((prevState, props) => ({
treats: prevState.treats +1
}));
}
Refactor the presentational component to use twoTreats() from props
const Puppies = (props) => {
return (
...
</div>
<h2>I want two treats!</h2>
<div>
<button
onClick={props.twoTreats}>Two treats please!
</button>
<div><h3>{props.treats}</h3></div>
</div>
...
);
}