React Fundamentals Flashcards

These questions cover a broad spectrum of React concepts and practices that are commonly tested during interviews.

1
Q

What is React?

A

React is a JavaScript library for building user interfaces, particularly single-page applications where you can create reusable UI components. It was developed by Facebook and is maintained by a large community of developers.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are components in React?

A

Components are the building blocks of a React application. They allow developers to split the UI into independent, reusable pieces. Components can be either class-based (using ES6 classes) or functional (using JavaScript functions).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the difference between state and props?

A

State: A local data storage that is mutable, i.e., it can be changed over time. State is used to manage the dynamic data within a component.

Props: Short for properties, props are read-only and used to pass data from one component to another, usually from a parent to a child component.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are React hooks?

A

Hooks are special functions introduced in React 16.8 that let you use state and other React features in functional components. The most commonly used hooks are useState and useEffect.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the useState hook?

A

The useState hook allows you to add state to functional components. It returns an array with two elements: the current state and a function to update that state.

const [count, setCount] = useState(0);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the useEffect hook?

A

The useEffect hook lets you perform side effects in functional components, like data fetching, subscriptions, or manually changing the DOM. It can be considered similar to lifecycle methods like componentDidMount, componentDidUpdate, and componentWillUnmount in class components.

useEffect(() => {
document.title = You clicked ${count} times;
}, [count]); // Dependency array controls when useEffect runs

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are controlled and uncontrolled components?

A

Controlled Components: Components where form data is handled by the React component’s state.

Uncontrolled Components: Components that store their own state internally, rather than relying on React state.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is React context?

A

React Context provides a way to pass data through the component tree without having to pass props down manually at every level. It is used for global state management, similar to Redux or other state management libraries.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is Redux, and how does it relate to React?

A

Redux is a state management library often used with React to manage complex application states. It uses a central store to hold the application’s state and allows components to access and update the state in a predictable way using actions and reducers.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the Virtual DOM, and how does React use it?

A

The Virtual DOM is a lightweight copy of the actual DOM. React uses it to optimize updates and rendering. When the state of an object changes, React updates the Virtual DOM instead of the real DOM directly. It then compares the Virtual DOM with a snapshot of the previous Virtual DOM and applies only the differences (a process called “reconciliation”).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is JSX?

A

JSX stands for JavaScript XML. It is a syntax extension for JavaScript that allows developers to write HTML-like syntax within JavaScript code. JSX makes it easier to write and visualise UI components.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the difference between componentDidMount and useEffect?

A

componentDidMount is a lifecycle method used in class components to run code after the component mounts.

useEffect is used in functional components to achieve similar behaviour. useEffect can run on every render by default, but with a dependency array, it can be configured to run only on mount, update, or unmount.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you optimise React applications?

A

React applications can be optimised by:

  • Using React.memo to prevent unnecessary re-renders.
  • Splitting components with React.lazy and Suspense.
  • Using useCallback and useMemo hooks to memoize functions and values.
  • Implementing pagination or infinite scroll for large data sets.
  • Avoiding anonymous functions in render methods to prevent re-creating them on every render.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are React fragments?

A

React Fragments allow you to group multiple elements without adding an extra node to the DOM. It is written as <React.Fragment></React.Fragment> or simply <> </>.

return (
<>
<h1>Title</h1>
<p>Description</p>
</>
);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is Prop Drilling and how do you avoid it?

A

Prop Drilling is the process of passing data from a parent component to deeply nested child components through props. It can make the codebase difficult to maintain. Context API or state management libraries like Redux can help avoid prop drilling by providing a central store for shared state.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the difference between useCallback and useMemo?

A

useCallback: Returns a memoized function, which helps prevent re-creating the function on every render.

useMemo: Returns a memoized value, used when you want to avoid recalculating an expensive value on every render.

16
Q

How does lifting state up work in React?

A

Lifting state up means moving state to a common parent component, so that the state can be shared between multiple child components. It helps in situations where several components need to reflect the same state.

17
Q

What are React portals?

A

React portals provide a way to render children into a DOM node that exists outside the DOM hierarchy of the parent component. It is often used for modals, tooltips, and overlays.

return ReactDOM.createPortal(

<ChildComponent></ChildComponent>

,
document.getElementById(‘portal-root’)
);

18
Q

What is the significance of key prop in React?

A

The key prop is used to uniquely identify elements in an array of components. It helps React in efficiently updating and rendering components when their order changes. Keys should be stable, unique, and constant.

19
Q

What are higher-order components (HOC)?

A

A higher-order component is a function that takes a component and returns a new component with additional props or behavior. HOCs are used to reuse component logic.

const withLoading = (Component) => (props) => (
props.loading ? <LoadingSpinner></LoadingSpinner> : <Component {…props} />
);