What function is the HTML-like syntax (JSX) in React transformed into?
It is transformed into the React.createElement function.
What does the React.createElement function return?
It returns a description object, also known as a React Element.
In a React Element object, what does the type property point to?
It points to a component, a memoized component, or a string representing an HTML tag.
When will React re-render an element if its type remains the same between renders?
It will re-render if the component referenced in type is not memoized with React.memo.
In a conditional render like {isCompany ? <Input /> : <TextPlaceholder />} what happens to the components when isCompany flips from false to true?
The TextPlaceholder component is unmounted, and the Input component is mounted.
In a conditional render {condition ? <Input /> : <Input />} with different props, why is the internal state of the Input preserved when the condition changes?
Because the component type (Input) is the same, React re-renders the existing component with new props instead of unmounting and mounting a new one.
React avoids direct DOM manipulation by creating and modifying a tree of objects commonly known as the _____.
Virtual DOM
In React’s Virtual DOM, what is the value of the type property for a standard HTML element like <input>?
The value is a string with the HTML tag name, e.g., ‘input’.
In React’s Virtual DOM, what is the value of the type property for a React component like <MyComponent />?
The value is a reference to the component’s function itself (e.g., MyComponent).
During an initial render, what does React do when it encounters an element whose type is a function?
It calls the function (the component) and then iterates over the tree that the function returned.
During a re-render, what is the primary property React compares between the ‘before’ and ‘after’ element objects to decide its action?
React compares the type property.
If an element’s type changes between re-renders, what two actions will React perform?
React will unmount the ‘previous’ component and mount the ‘next’ component.
What is the primary reason it’s considered an anti-pattern to define a component inside another component’s render body?
The inner component function is recreated on every re-render of the parent, resulting in a different function reference each time.
What is the consequence for reconciliation when a component’s type is a different function reference on every render?
React sees the type as having changed, causing it to unmount the old component and mount a new one on every single re-render.
The unintentional process of a component being destroyed and recreated on every re-render is called _____.
re-mounting
What are two negative consequences of unintentional re-mounting?
It is terrible for performance and causes the loss of all associated state and DOM elements (like focus).
In the ‘mysterious bug’ example, why did {isCompany ? <Input /> : <Input />} preserve state?
Because the type property for both conditional branches was a reference to the exact same Input component function.
When React encounters an array of children during reconciliation, how does it compare the elements between renders by default?
It compares elements based on their position (index) in the array.
How can you fix the ‘mysterious bug’ where two different Input components share state by altering their position in the render output?
By structuring the conditional render so the components occupy different positions, like {isCompany ? <Input /> : null}{!isCompany ? <Input /> : null}.
What is the primary purpose of the key attribute in React?
It provides a stable and unique identity for an element within an array of children across re-renders.
When rendering a dynamic list, if the key and type of an element match between renders, what will React do?
React will re-use the existing component instance, with all its associated state and DOM, regardless of its position in the array.
If you reorder a list of stateful components that do not have keys, what observable bug might occur?
The component’s internal state will remain with its original position (index) rather than moving with the data item it represents.
True or False: The key attribute is primarily a performance optimization that prevents list items from re-rendering.
False; key is for identity and instance re-use, while React.memo is used to prevent re-renders.
To prevent re-renders of items in a list, you should wrap the item component in _____.
React.memo