React Questions Flashcards
Learn
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.