Beginner Flashcards
Cover React Beginner Questions
How to render HTML in React?
You can use dangerouslySetInnerHTML
prop to render HTML in React. It is used to set HTML directly from React. You should be careful while using this property as it can cause XSS attacks.
What is the purpose of key
attribute in React?
The string attribute key is a special attribute you need to include when rendering an array of elements. Keys help React identify which items have changed, are added, or are removed. Keys should be given to the elements inside the array to give the elements a stable identity.
What is the difference between state and props?
Props are used to pass data from parent to child. They are like function arguments. They are immutable.
State is used to store component data. It is managed within the component and is mutable.
What is the naming convention for React components?
In React, the naming convention for components is to use PascalCase, meaning the first letter of each word in the component’s name should be capitalized.
For example, UserProfile, SidebarItem, or NavigationMenu. This convention differentiates custom React components from regular HTML tags in JSX, as React treats elements starting with a lowercase letter as DOM tags and those starting with a capital letter as custom components.
Why shouldn’t you use index
as a key in React lists and iterators?
Using index as a key can negatively impact performance and may cause issues with the component state. When the list items change due to additions, deletions, or reordering, using indexes can lead to unnecessary re-renders or even incorrect UI updates.
React uses keys to identify elements in the list, and if the key is just an index, it might reuse component instances and state inappropriately. Especially in cases where the list is dynamic or items can be reordered, it’s recommended to use unique and stable identifiers as keys to ensure consistent behavior.
What is the difference between Real DOM and Virtual DOM?
Virtual DOM is the representation of a UI in the form of a plain javascript object. It is a node tree that lists the elements, their attributes and content as Objects and their properties.
Real DOM is the real representation of a UI which can be seen and inspected in the browser. Manipulating the virtual DOM is much faster than real DOM, because nothing gets drawn on the screen. React uses this virtual DOM to figure out the most efficient way to update the browser DOM.
What’s the component’s lifecycle in React?
In React functional components, lifecycle-like behaviors are achieved using hooks:
Mounting and Unmounting:
Utilizing the useEffect hook with an empty dependency array ([]) ensures the hook runs after the component mounts to the DOM.
The cleanup function returned within the useEffect callback offers a mechanism for handling tasks when the component is about to unmount.
Updates:
The useEffect hook, when invoked without a dependency array or with specific dependencies, executes after every render or when specified prop/state changes are detected.
What is the difference between stateful and stateless components?
The main difference between stateful and stateless components is one has state and the other doesn’t.
Stateful components keep track of changes to their state and re-render themselves when the state changes.
Stateless components, on the other hand, render whatever is passed to them via props or always render the same thing.
Can we change the state of the component directly?
No, we can’t change the state of the component directly. State can only be changed by using setState()
method. Changing the state variable directly won’t re-render the component.
What is JSX?
JSX is a syntax extension to JavaScript and comes with the full power of JavaScript.
It lets you write HTML-like markup inside a JavaScript file.
JSX produces React “elements”.
You can embed any JavaScript expression in JSX by wrapping it in curly braces. After compilation, JSX expressions become regular JavaScript objects.
How do React Server Components handle data fetching?
Server Components can directly access backend resources, databases, or filesystems to fetch data during rendering, eliminating the need for a separate API layer for data fetching.
What are the features of React?
Use of Virtual DOM instead of Real DOM, JSX, Server-side rendering, Unidirectional data flow or data binding, Reusable components, etc.
What are React Hooks?
Hooks allow function components to have access to state and other React features.
Hooks are functions that let you “hook into” React state and lifecycle features from function components.
Hooks don’t work inside classes — they let you use React without classes. Some common hooks include useState, useEffect, useMemo, useRef, useCallback, etc.
How do Server Components differ from Client Components?
Server Components are rendered on the server and do not require client-side JavaScript for rendering.
While Server Components and Client components can coexist in the same app, Server Components can import and render Client components.
What is the difference between controlled and uncontrolled components?
A Controlled Component takes its current value through props and notifies changes through callbacks like onChange.
A parent component “controls” it by handling the callback and managing its own state and passing the new values as props to the controlled component.
An Uncontrolled Component stores its own state internally, and you query the DOM using a ref to find its current value when you need it. This is a bit more like traditional HTML.