React Questions Flashcards
Learn (33 cards)
When does ComponentDidMount occur?
ComponentDidMount occurs right after Render(), but it is only called the on initialization
What method do all components have to have?
The only method that a component has to have to Render()
What does setState() do?
SetState reconciles the new state object with the previous state. The purpose of this is to most efficiently update the DOM with only the necessary updates. Any components that have been changed will update.
What is a component?
A component is a function or class that ultimately creates an element, which is what you see on the screen.
What are refs?
Refs give you direct access to a DOM element. You create a ref and then pass it as a property of an element.
What are keys?
Keys help react keep track of elements in a list. Each element must have a unique key among siblings.
What is this.props.children?
What you place in between the element tags of a component, ie something can be accessed by the component via this.props.children. This can also be a component or a function.
Controlled vs Uncontrolled Components
Controlled vs Uncontrolled components. Controlled components are entirely controlled by React. For example, form data is stored in the state. This allows for instant validation or button enabling/disabling. Uncontrolled components allow the data to remain in the DOM and the data is just fetched whenever it’s needed.
When should you fetch initial data?
Fetching data should occur in the ComponentDidMount method. If your fetch resolves before the component mounts, you be setting state on a component that does not exist yet.
Why use React.Children.map(props.children, () => ) over props.children.map()?
Props.children is an object if there is only one child, and it is an array if there are multiple children. You favor React.Children.map(props.children, () => ) over props.children.map() because the latter will break if the children are not an array.
What does React.cloneElement() do?
React.cloneElement() is used to clone an element and pass it new props
What is the second parameter of setState()?
setState’s second parameter is an optional callback function as it is async. This is however rarely used
Besides an object, what else can you pass into setState()?
You can pass a function into setState. It is actually recommended if you are setting the new state based on the previous state.
What are stateless components?
Stateless/functional components cannot use state or lifecycle methods. They’re useful for presentational components. They are simple, easy to test, and decoupled from the app. The only drawback is that you cannot define the shouldComponentUpdate method so it COULD be costly to your rendering, but probably not in a small scale.
What are pure components?
PureComponents are good if your state and props are have a shallow data structure, as in there are no nested data structures. PureComponents will only rerender if there are shallow differences between prevState/prevProps and the new state/props.
What are components?
Components are more expensive than PureComponents, as they will run a full reconciliation. This should be the default over PureComponents as the optimizations aren’t necessary most of the time. Optimization should be added as needed over from the very beginning
What is shallow copying?
By default objects are shallow copied. Anything nested in the object are copied as pointers. Therefore, making changes to the copy will make changes to the original
What are the 4 categories of lifecycle methods?
Mounting, Updating, Unmounting, Error-Handling
What are the mounting lifecycle methods? In order.
Constructor, getDerivedStateFromProps, render, componentDidMount
What are the updating lifecycle methods? In order
getDerivedStateFromProps, shouldComponentUpdate, render, getSnapShotBeforeUpdate, componentDidUpdate
What are the unmounting lifecycle methods? In order.
componentWillUnmount
What are the error-handling lifecycle methods? In order.
getDerivedStateFromError, componentDidCatch
constructor()?
constructor() - This is when the component is initialized. This occurs before it is added to the dom. This is where you can bind methods and it is the only place you can directly change the state. (You must use setState() anytime after this.) This is not where you want to set up subscriptions or any event handlers as this occurs before the component is mounted.
static getDerivedStateFromProps(props, state)?
static getDerivedStateFromProps(props, state) - This is not commonly used and when it is, it may not be the right tool. Basically, if you need to update the state based on changes to the props. It is called here before render and is first to be called when a component updates.