React Flashcards

1
Q

What does the constructor method do ?

A

This method is called when the component is first created, and is used to initialize state, bind event handlers, and perform other setup tasks.

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

Which React component phase includes the invoking of the constructor function ?

A

Mounting

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

getDerivedStateFromProps(props, state)

A

This method is called every time a component is re-rendered, and is used to update the component state based on changes to the props.

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

Which phase includes invoking of getDerivedStateFromProps(props, state)

A

Mounting and Updating

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

Which react component phase includes invoking of the render function ?

A

Mounting and Updating

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

Which react component phase includes invoking of the componentDidMount method ?

A

Mounting

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

purpose of the render method ?

A

This method is called every time a component needs to be rendered, and returns the JSX to be rendered to the DOM.

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

purpose of component did mount method ?

A

This method is called immediately after the component is inserted into the DOM, and is used to perform any setup tasks that require access to the DOM, such as initializing third-party libraries or setting up event listeners.

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

What happens in the Mounting Phase of a React component ?

A

Mounting phase: These methods are called when an instance of a component is being created and inserted into the DOM.
- constructor()
- getDerivedStateFromProps(props, state)
- render()
- componentDidMount()

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

purpose of shouldComponentUpdate(nextProps, nextState):

A

This method is called before a component is re-rendered, and can be used to prevent unnecessary re-renders by returning false.

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

purpose of getSnapshotBeforeUpdate(prevProps, prevState):?

A

This method is called immediately before a component is re-rendered, and can be used to capture some information from the DOM (such as scroll position) before it is updated.

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

purpose of componentDidUpdate(prevProps, prevState, snapshot) ?

A

This method is called immediately after a component is re-rendered, and is used to perform any necessary cleanup or additional setup tasks.

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

What happens in the Updating Phase ? Which methods are called / invoked ?

A

These methods are called when a component is being re-rendered due to changes in its props or state.
* getDerivedStateFromProps(props, state):
* shouldComponentUpdate(nextProps, nextState):
* render():
* getSnapshotBeforeUpdate(prevProps, prevState):
* componentDidUpdate(prevProps, prevState, snapshot):

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

purpose of componentWillUnmount() ?

A

This method is called just before a component is removed from the DOM, and is used to perform any necessary cleanup tasks, such as removing event listeners or canceling network requests.

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

What happens in the unmounting phase ?

A

Unmounting phase: This method is called when a component is being removed from the DOM.
—- componentWillUnmount()

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

purpose of useEffect Hooks

A

to handle side effects in functional components

17
Q

What are the two arguements that the useEffect hook takes in ?

A

a function that represents the side effect to be performed, and an array of dependencies that determine when the effect should be executed.

18
Q

When will the useEffect Hook get executed ?

A

after the component is rendered and whenever any of the dependencies in the second argument change.

19
Q

What would happen if you omit the second argument to the useEffect Hook ?

A

the effect will be executed after every render.

20
Q

Which lifecycle methods will the useEffect hook correspond to ?

A

the componentDidMount, componentDidUpdate, and componentWillUnmount lifecycle methods in class components.

21
Q

How does the useEffect hook undergo the componentDidMount lifecycle method ?

A

When the component is first mounted, the effect function is executed after the first render.

22
Q

How does the useEffect hook undergo the componentDidUpdate lifecycle method ?

A

When the component updates, the effect is executed again if any of the dependencies have changed.

23
Q

How does the useEffect hook undergo the componentWillUnmount lifecycle method ?

A

when the component is unmounted, the effect’s cleanup function is executed.

24
Q

How does the useEffect support a cleanup function ?

A

useEffect also supports a cleanup function that allows developers to clean up any resources that were created by the effect. This function is returned from the effect function and is executed when the component unmounts or when the effect is re-executed due to a change in the dependencies.

25
Q

What are error boundaries ?

A

Error boundaries are components that catch JavaScript errors anywhere in their child component tree and display a fallback UI instead of the component tree that crashed.

26
Q

How would you define an error boundary ?

A

To create an error boundary component, you need to define the componentDidCatch lifecycle method.

27
Q

What is the purpose of react error handling APIs ?

A

React also provides a set of error handling APIs that can be used to handle errors that occur during rendering or other lifecycle methods.

28
Q

which lifecycle methods can use the react error handling APIs?

A

getDerivedStateFromError, componentDidCatch, and setState to update component state based on the error.

29
Q

What features fo third-party error handling tools provide in the context of react ?

A

These tools can provide detailed information about the error, such as the stack trace and environment variables, to help you diagnose and fix the issue quickly.

30
Q

Name 3 third-party error tracking tools

A

Sentry, Rollbar, or Bugsnag

31
Q

Enlist 4 ways of handling errors in your react application

A
  • Error Boundaries:
  • try-catch blocks:
  • React Error Handling API:
  • Third-party error tracking tools:
32
Q

If you have large amount of data in a react application, what are some strategies that can be deployed to optimize the performance of such an application ?

A

When dealing with large amounts of data in a React application, it’s important to optimize the performance of your code to ensure that it remains fast and responsive for your users. Here are a few tips on how to do this:
1. Use pagination or infinite scrolling: Instead of rendering all of your data at once, consider using pagination or infinite scrolling to load data incrementally as needed. This can help to reduce the amount of data that needs to be processed and rendered at any given time.
2. Use virtualization: Virtualization is a technique where only the visible portion of a large list or table is rendered, while the rest is stored in memory or on disk. This can help to reduce the amount of data that needs to be processed and can improve the overall performance of your application.
3. Use memoization and pure components: Memoization and pure components can help to reduce the number of re-renders that occur when data changes. By only re-rendering the components that actually need to be updated, you can reduce the overall workload of your application and improve its performance.
4. Avoid unnecessary calculations: Make sure that your code is only performing necessary calculations and operations. This can be achieved by avoiding unnecessary state updates, optimizing algorithms, and reducing the complexity of your code.
5. Use Web Workers: Web Workers are a way to run background tasks in a separate thread, which can help to reduce the workload of your main thread and improve the overall performance of your application.
6. Use the React Profiler: The React Profiler is a tool that allows you to measure the performance of your React components and identify areas that need optimization. By using this tool, you can identify performance bottlenecks and make targeted optimizations to improve the overall performance of your application.
7. Lazy loading images in React - To optimize an application that consists of several images, we can avoid rendering all of the images at once to improve the page load time. With lazy loading, we can wait until each of the images is about to appear in the viewport before we render them in the DOM.
8. Code-splitting in React using dynamic import() - Code-splitting is another important optimization technique for a React application. By default, when a React application renders in a browser, a “bundle” file containing the entire application code loads and serves to users at once. This file is generated by merging all the code files needed to make a web application work.
By implementing these techniques, you can help to ensure that your React application remains fast and responsive even when dealing with large amounts of data.