React Flashcards
What is redux?
Redux is a state manager. It can be used as a data store. It allows us to take state and make it globally accessible. We can subscribe any component to a state in the redux data store.
How does redux work?
Redux works as a state container that allows us to then subscribe states to as many components as we wish. From these components we can then dispatch actions that mutate state.
These actions can be saved within redux store folders and all the dispatches can be saved as cases in a reducer.
This allows us to scale large apps easily.
What makes Redux so special?
Without a state management tool like redux it would be very difficult to scale apps to large sizes. Problems such as prop drilling would be way too prevalent and make designing large scale apps difficult.
Redux set up process
To setup redux we npm install react-redux
We setup a folder structure with the store, reducers and actions.
Next we have an index.js in the store where we combine all reducers and create a store with.
This store we then wrap in our app with
To subscribe our redux store to a component we export default connect, with the name of the component in brackets, in the connect function we also mapstatetoprops or mapdispatchtoprops
There we import which functions we want and what state we need
This wraps the component with the redux store and subscribes it to state changes
What are reducers in redux?
As the name suggests, a reducer is something that reduces all possible actions within it to one result.
Reducers in redux are usually written with switch/case syntax where every case is easily readable and is obvious what kind of state will come out as result.
Reducers easily describe what happened to state in an action.
Why did you choose React?
Personally I chose react because it’s very lightweight and there is a heavy demand for it in the industry. After i taught myself fundamentals of JS
With angular I would have had a steeper learning curve but with React I can install packages as I see the need for them and smoothly expand my learning curve as it fits me.
I understand Angular as a framework is a complete package while with React you need to install packages to expand its functionalities.
React is efficient, it’s declarative, all the complex stuff in creating UI’s looks easily readable in React unlike Angular for example.
What is AJAX?
AJAX is Asynchronous Javascript And XML, it is the use of the XMLHTTPrequest object to communicate with servers. It can send and receive information in various formats.
AJAX allows us to not have to reload a page to update its content
AJAX is a technology concept while most people don’t think about it nowaday and use promise based http libraries like axios which handles it internally for us.
What are the major features of React?
JSX - Javascript Syntax Extension Component based architecture Declarative UI Virtual DOM Smooth learning curve
What is JSX?
Javascrypt Syntax Extension or Javascript XML
allows us to write HTML in React
JSX converts html tags to react elements
What is virtual DOM? How does it work?
Virtual DOM is a representation of the UI kept in memory and synced with the real DOM.
This allows us skip traversing the DOM to change an element as we can simply change an element in virtual DOM as though its an object than re-render only the change
Virtual DOM isn’t necessarily faster but it saves us a lot of time by doing all the manual work of appending and creating elements.
Virtual DOM also uses key attributes for elements in lists so it doesnt have to repaint the whole list only the element that changed.
props vs state?
Props are used to pass data from one component to another, these can be functions, variables, objects, arrays, pretty much anything
The state is a local data storage that is local to the component and cannot be passed around.
What does lifting state mean and why do we do it?
Let’s say we have many nested components, sometimes these components share one state, so to avoid unnecessary prop drilling between all these components we lift state to the highest order parent which then shares state to all components that need it in a tree
Why do we set a key property when we map over an array in React?
When we map over a list, we always set key properties for every item in list.
So that in the future when one of these items change, React can know exactly which item so it avoids re-rendering the entire list
What are the core principles of Redux?
Single source of truth
- global state of the app is stored within a single store
Read-only state
- state cannot be changed unless an action describing the change is dispatched
- we never directly change state, we only transform it
Changes are made with pure functions
- there are no side effects and the functions are declarative
How do you make an AJAX request using Redux?
We would need to apply middleware, in this case redux-thunk which allows us to do async functions