Redux Flashcards
When redux is useful? What are the other options?
Redux is useful to share data with different components.
props (lifting state), react context and redux
props is the simplest and redux the more powerful and complex.
What are the 3 principles of Redux?
1 - One immutable store;
2 - Actions triggers changes;
3 - Reducers update state;
What a Reducer consists of?
a method containing the current state, an action to change the state and it returns a new state
What is the philosophy of redux?
data goes down and actions go up
What is a Reducer?
A pure function
What are the three common ways to do immutable updates?
object.assign, spread operator and map/filter/reduce.
What is a common pitfall of immutability of nested objects? Is that always an issue?
The clone is shallow and won’t clone inner objects. Not always an issue because you only need to clone stuff you’ll actually change.
What is the danger in the clone-deep?
It clones everything and is likely to cause excessive re-renders.
What is the most common lib to have immutability out-of-the-shelf?
Immer
What is an action? What property the action must have?
Represents user intent. Must have a type. Can have anything else extra to the type…
When an action is dispatched, how many reducers will be notified?
All of them, always.
From redux perspective, what are the differences between container and presentation components?
in simple terms: only containers know redux
detailed here: https://pasteboard.co/JmfOcHE.png
When reselect is a good choice? what does it do?
when selectors (mapStateToProp) do expensive operations. it does memoization
What are the two most common ways to mapDispatchToProps?
using bindActionCreators or returning an object
Why binding in the constructor is better than binding directly in the onChange assignment?
Because binding in the assignment will cause a new function to be allocated everytime the component renders.
What is a class field and what does it do the bindings?
A class field is an arrow function that receives an event as argument. It removes the need of bindings.
Why an arrow function removes the need of bindings?
Because “Arrow functions inherit the binding context of their enclosing scope”. Therefore, the this keyword is actually referring to the scope - as you might expect.
What does a class field do the initialization of the state of a class? what is the main benefit?
it removes the need of defining constructor and also removes the need of “this.” keyword… less code!
Why react hooks is so attractive?
Because it pretty much removes the need of creating class components.
Why is it recommended to define the onSubmit event in the form instead of handling a button click?
For accessibility and usability reasons: the user will be able to hit enter and submit the form
What is an action creator? Can it be a anonymous object?
Is a function that creates an action object (remember, must have a type). Yes, as usual people use anonymous in js for everything.
Why is it important to always return a default in the reducer “type” switch?
Because all reducers are called, so if your reducer don’t care about the message that was dispatched, then it must return the same state it received.
What is normalizing state shape? where to find more information?
State shape is how the state object is defined… having it as id: 1 {id: 1, name:”asd”} helps during find operations. See normalizing state shape in redux website.
Can i use something else other than a switch in the reducers to filter out the action type?
Yes, anything you want.
Does the name of the import must match the export default function/type?
No. Once it is an export default, you can name it anything you want.
Besides the initial state and the root reducer, what else can be applied in the CreateStore function?
You can applyMiddleware
What is a common and recommended middleware to be applied in the redux store? Why?
reduxImmutableStateInvariant. So that it will warn you when someone mutate the store’s state.
What is redux dev tools? What is the necessary import from redux to make it work?
Is a tool that allows playing with the application’s state directly in the browser. Necessary to import “copose” from redux.
When is it a good idea to configure the store with a different initial state different than the default empty?
Seems to be more useful for server side to do some sort of re-hydrate.