React Flashcards
What is React?
React is a JavaScript library used to build user interfaces (UI components).
What is Redux?
Redux is a centralized state management tool. Using this single global object (called Store), allows any component to access state data without having to pass props through components that don’t need to access the data.
How does Redux work?
Redux uses the Store, Reducers, and Actions. The Store is the central location that contains the application’s state. Reducers are functions that take 2 parameters: current state and an action that then returns the current state of action. Actions are JavaScript objects that contain two properties: type and payload. The actions are “dispatched” or used as arguments by the Store’s dispatch API method. When the action is called it will run the reducer function, the state will be updated based on the action type.
Why did you choose React over Angular, Vue, or any other framework?
I chose React for a few reasons. First is it’s ability to break the UI down into reusable components. Next is the fact that it uses the virtual DOM to increase performance. Another reason is the availability of support, libraries, and its wide-spread use in the development community.
What are some major features of React?
JSX which is a syntax extension of JavaScript that allows you to embed HTML into JavaScript code.
The Virtual DOM is a copy of the DOM that is kept in memory and React only updates changed objects instead of all objects which makes the app faster.
Another is One-way Data Binding which is when the developer nests child components within parent components which makes it easier to find problems and debug errors.
What’s the difference between state and props
State is data that is internally controlled by the component and props are variables that are passed into from and controlled by a parent component.
What is lifting state up and why do we do it?
Lifting state up is the process of sharing state by moving it up to the closest common “ancestor” of the components that need it. We do this when multiple components need to share the same changing data.
Why do we set a key property when we map over an array in React?
The key helps React identify which items have changed, are added, or are removed. Keys need to be unique. Although it can be used, an index should not be used as the index of an item can change causing errors later.
What is redux-thunk?
Redux-thunk is a middleware that allows you to write action creators that return a function instead of an action. It can then be used to delay the dispatch of an action or to dispatch only if a certain condition is met.
What is the difference between a class and functional component?
Besides the way they are declared, class components can contain state while functional components cannot (unless you are using Hooks).
What are the different lifecycle methods in React?
Mounting: initialization of the component.
constructor() - called before anything else.
getDerivedStateFromProps() - called before rendering the elements in the DOM.
render() - returns HTML elements rendered inside the DOM.
componentDidMount() - called right after the component is rendered inside the DOM.
Updating: caused by changes in state or props and leads to re-rendering of the component.
componentDidUpdate() - Gets called after the component gets re-rendered.
getSnapshotBeforeUpdate() - called just before the newly rendered HTML gets committed to the DOM.
shouldComponentUpdate() - called before rendering the component when new props are received.
render() - returns HTML elements rendered inside the DOM.
getDerivedStateFromProps() - called again when a component is being re-rendered.
Unmounting: componentWillUnmount() - called just before the component gets destroyed.
What are Hooks?
Hooks let you use state and other React lifecycle features without writing a class component.
What are Higher Order Components?
Higher Order Components are functions that take in a component and return a new component.
What is the Redux setup process?
First you would set up the Store to contain any needed state data. Then you use the Provider component to make the Store available to the rest of your app. After this you can set up your action and reducer files. The actions will be set up as JavaScript objects that contain a type and payload to be “dispatched” by the application components or the Store’s dispatch API method. The reducers are functions that will take in 2 parameters: state and action. The reducer will in turn return the updated state (always a copy of the original state returned as a new state object - never mutate the original state). Using mapStateToProps() and mapDispatchToProps(), you can connect the actions and state to your components allowing your React component to communicate with your Redux Store.
What are the core principles of Redux?
There are 3:
- Single source of truth (meaning the global state of your application is stored in a single location - the Store)
- State is read-only (The only way to change state is through actions)
- Changes are made with pure functions (All reducers are pure functions meaning they take in the previous state and an action and return the next state)