Your state is: pretty RELAXED
Your state is: pretty nervous
This means state of your mind changes based on the situation
State is something
Due to user interaction.
You can think of 4 types of state
[
{name: "Lenovo", price: "$649.99", stock: 12, ...},
{name:"Mac the money sucker",price:"$1999.99",stock:8, ...},
{name: "Dongle", price: "$29.99", stock: 12, ...},
{name: "iPod Touch", price: "$699.99", stock: 1, ...},
{name: "iPhone X", price: "$999.99", stock: 31, ...},
{name: "Nexus 7", price: "$199.99", stock: 0, ...},
];
componentDidMount() {
Promise.all([
api.shop.getProducts(),
api.shop.getCurrentOrder()
]).then(([products, order]) => {
// No pre-processing of our data from the server
this.setState({ products, order });
});
}
getSelected(product) {
return this.state.order.find(p => p.id === product.id);
}
If possible calculate derived properties of your data during render()
toggleProductInOrder(product) {
const order = this.state.order;
const selectedProduct = this.getSelected(product);
//Either add or remove the item in the order list
const updatedOrder = selectedProduct
? without(order, product) : concat(order, product) ;
return api.shop.updateOrder(updatedOrder).then(newOrder =>{
this.setState({ order: newOrder }); //update state
});
}
Compute From states
class MyComponent extends Component {
constructor(props) {
super(props);
this.state = { message: props.message + "!" };
}
getDerivedStateFromProps(props) {
this.setState({ message: props.message + "!" });
}
render() {
return (
{this.state.message}
);
}
}
Are you using states where you don't have to?
Just read from the props/state
render() {
const excitedMessage = this.props.message + "!";
return ({excitedMessage});
}
Avoid using state for data which can be calculated from props or other state
(Apply these 3 rules)
cart
doesn't have selected product priceShop
Component to contain the state
this.state
which can be calculated from props/stateWhat is the name of the app?
Your manager tells you to
But Don't worry: the bigger is the mess, this higher is the job security
Will start knocking at your door
None of these is a must to build an app
Is redux a must?
Who was the guy knocking at the door?
When you need to share state a lot
{
type: 'ADD_TODO',
text: 'Take 8 bathroom breaks'
}
function add(x, y){
return x + y;
}
add(1, 2); // output is 3
add(1, 2); // output still is 3
add(1, 2); // WILL ALWAYS output 3
case ADD_TODO:
return [
...todos,
{
text: action.text,
completed: false
}
]
github.com/khan4019/redux-todo
Redux | MobX |
---|---|
single store | multiple stores |
flat state | nested state |
functional programming | OOP & reactive programming |
immutable | mutable |
plain JavaScript | “magic” JavaScript |
more boilerplate | less boilerplate |
How many guys were sleeping in the class room?
New cool kid in the corner since React 16.3
Don’t use context just to avoid passing props a few levels down.
Tight on food budget
Want to save more...
Save even more...
Ultimate saver...
-Slides: @jsDude005
-Website: thatjsude.com