Both props and state changes trigger a render update
Both props and state are deterministic. If your Component generates different outputs for the same combination of props and state then you're doing something wrong.
Stateless Component
Only props, no state.
There's not much going on besides the render() function and all their logic revolves around the props they receive. This makes them very easy to follow (and test for that matter).
We sometimes call these dumb-as-f*ck Components (which turns out to be the only way to misuse the F-word in the English language).
Stateful Component
Both props and state.
We also call these state managers.
They are in charge of client-server communication (XHR, web sockets, etc.), processing data and responding to user events. These sort of logistics should be encapsulated in a moderate number of Stateful Components, while all visualization and formatting logic should move downstream into as many Stateless Components as possible.
Props
Props are passed into the components
The property is passed to the component, similar to argument is passed to a function. In fact,
could be written as
```jsx
function Welcome(props) {
return <h1>Hello {props.name}</h1>;
}
Somehow, it means that props is passed from above. Almost, but not always. A component can also have default props, so if a prop isn’t passed through, it can still be set.
If `Welcome` is called without a `name` it will simply render `<h1> Hello world</h1>`
#### Props should not change
- Props contains information set by the parent component (although defaults can be set) and should not be changed
- React components that use only `props` are considered as `pure`, they render the same output given the same input (props that passed in)
### State
#### State is created in the component
```jsx
class Button extends React.Component {
constructor() {
super();
this.state = {
count: 0,
};
}
updateCount() {
this.setState((prevState, props) => {
return { count: prevState.count + 1 }
});
}
render() {
return (<button
onClick={() => this.updateCount()}
>
Clicked {this.state.count} times
</button>);
}
}
The constructor is where state gets its initial data. The inital data can be hard coded as above (count: 0), but it can also come from props.
State is changeable
State contains “private” information for the component to initialise, change, and use on it’s own
Common ground
props
andstate
are plain JS objectsprops
andstate
changes trigger a render updateprops
andstate
are deterministic. If your Component generates different outputs for the same combination ofprops
andstate
then you're doing something wrong.Stateless Component
props
, nostate
.Stateful Component
props
andstate
.Props
Props are passed into the components
const element = ;
Welcome.defaultProps = { name: "world", };
The
constructor
is where state gets its initial data. The inital data can be hard coded as above (count: 0), but it can also come from props.State is changeable
We change the
state
to keep track of the total number of clicksDo not use
React cannot listen to the state getting updated in this way, so your component will not re-render
Do not use
This does not take into account the asychronous nature that
setState
can use and might cause errors.Do use
onClick={() => this.updateCount()}
means that when the button is clicked theupdateCount
method will be calledupdateCount
will have access to this instance’s stateThe State flow
The component is initialised and
state.count
is set to 0The component renders, with Clicked 0 times as the button text
The user clicks the button
updateCount
is called, bound to this instance of the componentupdateCount
callssetState
with a call back to increase the counter from the previous state’s counter valuesetState
triggers a call to render >> MAGIC !!!The component renders, with Clicked 1 times as the button text