REACT / REDUX Flashcards
What is React? When and why would you use it?
React is a component based front-end framework that gives us the power to efficiently manipulate the DOM and create interactive User Interfaces. React, like other front end frameworks, allows the developer to manipulate the DOM with significantly less code while also creating a system that makes it easier to organize and manage various parts of the UI. React does this in a unique way by treating each UI element as a separate component that integrate the JavaScript, HTML, and CSS in one place in order to make the intended rendered element easier to manage and even reuse throughout the application.
What is Babel?
Babel is a transpiler that will take any javascript code and convert it into an older version of the code in order to manage any problems that may arise with older browsers. It also has the powerful benefit of working with JSX code, making it an essential tool for building React apps which are all a combination of HTML and JavaScript (which the browser can’t read).
What is JSX?
JSX is a JavaScript extension that combines JavaScript and HTML syntax in the same document. It is used in React as the main way to create components which are a bundle of the visual aspect (the HTML) and the logical (the JavaScript). JSX is not readable by any browser, so a transpiler like Babel will be necessary in order to view the page. Because of this, when using Babel, JSX code will be readable by nearly every browser.
How is a Component created in React?
Components let you split the UI into independent, reusable pieces, and think about each piece in isolation. Conceptually, components are like JavaScript functions. They accept arbitrary inputs (called “props”) and return React elements describing what should appear on the screen. Components have properties (immutable data that can be used in rendering the component) and state (mutable data that is managed by the component itself). A component may also have various effects (defined within the function) and hooks (usually defined outside the function and even in separate files).
What are some difference between state and props?
the major difference is that props are immutable while state is mutable. Props are either defined by default or passed in by parent components, while state is defined and managed within the component.
What does “downward data flow” refer to in React?
Downward data flow is the concept in React that data can only be passed from parent components to child components, not the other way around. This can make React apps a bit easier to comprehend and debug since there will always be a sort of “nested” direction of data flow.
What is a controlled component?
a controlled component is any element whose state is controlled by the React state. For example, HTML form inputs are often maintaining their own state dependent on what a user inputs. In order to control this, we would need to set the value of these inputs to the state in React and update that value only when some setState function has been used to rerender the component. This is usually done with some sort of handleChange
function that sets react state onChange
(any time the input value changes).
What is an uncontrolled component?
an uncontrolled component is one that stores changes in the HTML DOM instead of in Reacts virtual DOM. This usually means that React will set the original state with a defaultValue
tag and use a “ref” in order to hold on to and utilize that data.
What is the purpose of the key
prop when rendering a list of components?
the key prop is crucial for react to know which components have actually been manipulated, especially in a list of components that are mostly or event completely the same. The key must be unique for each component in order to accomplish this. The key prop is used by React, but will not be usable as a prop for the developer or even seen by the final rendered DOM at all.
Why is using an array index a poor choice for a key
prop when rendering a list of components?
using the array index can be cause issues in the way React interacts with the components if the order of the list eventually changes for any reason.
Describe useEffect. What use cases is it used for in React components?
useEffect is a useful hook in react that allows you to program in a side effect of an action that will run after the component is rendered. These effects will run after the first render and after every subsequent render unless defined by the second argument which is an array of state. This array of state is used to inform the effect to only run after one or more of those pieces of state has been updated. These can be useful for doing an initial AJAX request when the item first renders or for updating a separate piece of state when the first is changed (i.e. counters or toggles).
What is the purpose of the React Router?
React Router gives the ability for our website to appear to work like a traditional website with multiple pages and url addresses, but it is actually a single page application. It provides functionality for the URL bar, bookmarks, and back/forward buttons.
What is a single page application?
This is an app that serves all the HTML that the client sees from one static HTML page that is manipulated by the JS. This usually means that any routing in the app will be done on the client side and no actual server requests will be made to generate the page when changing routes.
What are some differences between client side and server side routing?
With server-side routing, HTML is rendered by the server upon each HTTP request from the client. With client side routing, all routing and HTML rendering in the application is handled by the JavaScript on the client side and requests to the server tend to only return JSON instead of HTML.
What are two ways of handling redirects with React Router? When would you use each?
with React Router, there is a Redirect
component that simply re-routes the user to a chosen route and can be used in the Route
components or in the return statement of another component. This is mostly useful when a user attempts to go to a route that is protected or does not exist. There is also a history object that we have access to with the useHistory
hook in ReactRouter that will send the user to this new route and add it to the browser history ( -history.push("/redirect-route")
-). This is more usefull as a final action inside of a callback function ( -i.e. when a form is submitted so the user can be redirected if the submit is successful -).
What are two different ways to handle page-not-found user experiences using React Router?
- One way is using
Switch
to wrap all your routes and adding aNotFound
component in a route listed last.Switch
will search for the first route that matches the user inputed url, so if none are found, theNotFound
component will be rendered ( -so long as theRoute
component wrapping theNotFound
componenet does not have anexact
keyword or any specification of a route -).- Another way would be to simply us a
Redirect
component when the user reaches a wrong path that sends the user somewhwere else ( -whether that be the 404 page or another desired route -).
- Another way would be to simply us a
How do you grab URL parameters from within a component using React Router?
you can use the useParams()
hook which grabs all the url params in an object of key value pairs.
What is context in React? When would you use it?
context in React gives us the ability to pass props down to any nested component by defining them in a singular ancestral component and wrapping the first nested component(s) with a .Provider
property on the context object created from useContext()
. This is very useful when certain props need to be used by a multitude of components that may not be sibling components or are nested beyond the child component of the parent component that defines the props.
Describe some differences between class-based components and function components in React.
- class-based components require more setup than their function counterparts because of their need to establish props and state in a constructor method (or as instance properties). Because class components utilize OOP, there is also a lot of work with
this
, meaning instance methods will need this binding to maintain the proper context.- Another major difference is the methods used to manage the component life cycle. Class-based components have a lot of different methods to manage each part of the component life cycle (
componentDidMount
,componentDidUpdate
,componentWillUnmount
,render
, etc.) while function components handled most of this with a few simple hooks (namelyuseEffect
which can manage mounting, updating and unmounting components in one function).
- Another major difference is the methods used to manage the component life cycle. Class-based components have a lot of different methods to manage each part of the component life cycle (
What are some of the problems that hooks were designed to solve?
- Hooks initially solved a major problem with function Components which was that they had no way of managing state and were simply used for presentational purposes.
- Another big problem hooks helped solve was the duplication of code within a component where a lot of similar logic was happening throughout the lifecycle of a component that had to be repeated in each different lifecycle function. Now, the hooks allow you to hook into the state in a way that allows you to manipulate state throughout the lifecycle from one central hook.
- similarly, hooks allow for cross component usage so that logic can be stored in a single hook and used throughout the app.
What is Redux? Why might you use it?
-
Redux
is a tool that allows for centralized state management throughout an application. Redux has the concept of a -“store” - that houses all of the state for the application and can be accessed and updated from almost anywhere.- This can be very useful, especially in React applications where it can be very challenging to pass state around the application without -“prop-drilling” - (passing state down through various child components to reach a deeply nested component) and manage state when state may live in various components throughout the application. While React now has some useful hooks for managing this without Redux, Redux is still widely used because of some other great features like the Redux developer tools.
What are three features of the Redux developer tool in Chrome?
- Analyze every action and see how state changes
- Move around in time to see how state changes
- Record actions and play back later
- see entire state tree
What is a store (in Redux)?
a store is a Redux feature that acts as the centralized location for the state in an application. There are specific rules and methods for accessing and updating this state throughout the application, making it easier to manage state changes. The store is updated with a reducer that is trigger via an action.
What is a reducer?
A reducer is a pure function that takes in the existing state and an action, updates the state as needed, and returns the new state. The action object will always have a key of “type
” which will be used to dictate to the reducer what it should do to the state and what it will return.