Dynamic information is information that can change.
React components will often need dynamic information in order to render.
For example, imagine a component that displays the score of a basketball game.
The score of the game might change over time, meaning that the score is dynamic.
Our component will have to know the score, a piece of dynamic information, in
order to render in a useful way.
There are two ways for a component to get dynamic information: props and state.
Besides props and state, every value used in a component should
always stay exactly the same.
Unlike props, a component’s state is not passed in from the outside.
A component decides its own state.
You can take a look at w3schools document on React State
Here
constructor() and super() are not unique to React.
The constructor() is a special method for creating and initializing
objects created within a class.
Here you can see more from w3schools about...
constructor()
super()
Stateful Components are any component that has a state property.
Stateless Components are any component that does NOT have a state property.
import React from 'react'; import ReactDOM from 'react-dom'; const green = '#39D1B4'; const yellow = '#FFD712'; class Toggle extends React.Component { constructor(props) { super(props); this.state = { color: green }; this.changeColor = this.changeColor.bind(this); } changeColor() { if (this.state.color == green) { this.setState({ color: yellow}) } else { this.setState({ color: green}) } } render() { return ( <div style={{background:this.state.color}}> <h1> Change my color </h1> <button onClick={this.changeColor}> Change color </button> </div> ); } } ReactDOM.render( <Toggle />, document.getElementById('app') )
constructor(props) { super(props); this.state = { color: green }; this.changeColor = this.changeColor.bind(this); this.myName = "Gary"; }For properties that are created inside the class we need the constructor & super as props means 'properties'.
changeColor() {
if (this.state.color == green) {
this.setState({
color: yellow})
} else {
this.setState({
color: green})
}
}
To create a method in the class you need to, eg...
changeColor() { if (this.state.color == green) { this.setState({ color: yellow}) } else { this.setState({ color: green}) } }Just a side comment
changeColor() { const newColor = this.state.color == green ? yellow : green; this.setState({color: newColor}) }This way uses the Ternary Operator expression , eg...
changeColor() { if (this.state.color == green) { this.setState({ color: yellow}) } else { this.setState({ color: green}) } }Once the properties have been set-up inside this.state you can change their values by, for example, in the changeColor() method.
this.setState() is actually two things:
this.setState(), immediately followed by .render()
Which is why you can’t call this.setState() from inside
of the .render() method! this.setState() automatically calls .render().
If .render() calls this.setState(), then an infinite loop is created.
render() {
return (
<div style={{background:this.state.color}}>
<h1>
Change my color
</h1>
<button onClick={this.changeColor}>
Change color
</button>
</div>
);
}
NOTE that in the render method of the class we used
double curly braces { {} } inside the <div> element.