Props:
State:
setState({someNew: 'data'})
state = {value: 'something'}
<body>
<div id="root"/>
</body>
<script>
/* This is a perfectly valid React Component*/
const HelloThere = (props) => <h1>Hello, { props.name }</h1>;
ReactDom.render(<HelloThere name="Grace Hopper"/>,
document.getElementById('root')
)
</script>
<body>
<div id="root">
<!-- This is what the code would result in -->
<h1>Hello, Grace Hopper</h1>
</div>
</body>
/* Always returns the same JSX for given inputs */
const UserInfo = (props) => {
return (
<div className="UserInfo">
<Avatar user={ props.user } />
<div className="UserInfo-name">
{ props.user.name }
</div>
</div>
);
}
/* This is a 'Pure' function */
/* It always returns the same output for given inputs */
function sum (first, second) {
return first + second;
}
/* This is an 'Impure' function */
/* It can return different things based on the balance */
function deduct (bankAccount, amount) {
bankAccount.balance = bankAccount.balance - amount;
return bankAccount
}
Null
this.setState()
this.setState()
See the Pen React Clock Example by Joshua Burke (@Dangeranger) on CodePen.
function tick() {
const element = (
<div>
<h1>Hello, world!</h1>
<h2>It is {new Date().toLocaleTimeString()}.</h2>
</div>
);
ReactDOM.render(
element,
document.getElementById('root')
);
}
setInterval(tick, 1000);
Does this use component state?
/* Now we have a component */
function Clock(props) {
return (
<div>
<h1>Hello, world!</h1>
<h2>It is {props.date.toLocaleTimeString()}.</h2>
</div>
);
}
/* Use the component in a function */
function tick() {
ReactDOM.render(
<Clock date={new Date()} />,
document.getElementById('root')
);
}
setInterval(tick, 1000);
/* From this */
function Clock(props) {
return (
<div>
<h1>Hello, world!</h1>
<h2>It is {props.date.toLocaleTimeString()}.</h2>
</div>
);
}
/* To this */
class Clock extends React.Component {
render() {
return (
<div>
<h1>Hello, world!</h1>
<h2>It is {this.props.date.toLocaleTimeString()}.</h2>
</div>
);
}
}
class Clock extends React.Component {
constructor(props) {
/* Make sure to call super in a constructor */
super(props);
/* Setting the initial state */
this.state = {date: new Date()};
}
render() {
return (
<div>
<h1>Hello, world!</h1>
<!-- Use the state instead of props -->
<h2>It is {this.state.date.toLocaleTimeString()}.</h2>
</div>
);
}
}
class Clock extends React.Component {
constructor(props) {
super(props);
this.state = {date: new Date()};
}
componentDidMount() {
this.timerID = setInterval(
() => this.tick(),
1000
);
}
componentWillUnmount() {
clearInterval(this.timerID);
}
tick() {
this.setState({
date: new Date()
});
}
render() {
return (
<div>
<h1>Hello, world!</h1>
<h2>It is {this.state.date.toLocaleTimeString()}.</h2>
</div>
);
}
}
See the Pen Hello World in React by Joshua Burke (@Dangeranger) on CodePen.
setState({some: 'state'})
/* Not good */
this.state.comment = 'Hello';
/* Excellent */
this.setState({comment: 'Hello'});
setState()
schedules an update/* Incorrect */
this.setState({
counter: this.state.counter + this.props.increment,
});
/* Super Great */
this.setState((prevState, props) => ({
counter: prevState.counter + props.increment
}));
constructor(props) {
super(props);
this.state = {
posts: [],
comments: []
};
}
componentDidMount() {
fetchPosts().then(response => {
this.setState({
posts: response.posts
});
});
fetchComments().then(response => {
this.setState({
comments: response.comments
});
});
}
See the Pen Hello World in React by Joshua Burke (@Dangeranger) on CodePen.
/