React Flashcards

1
Q

How would you explain the concept of the Virtual DOM to a non-technical person?

A

The Virtual DOM is a concept used in React, a popular JavaScript library for building user interfaces. It is a representation of the actual DOM (Document Object Model) in memory, which is a tree-like structure that contains all the elements of a web page. The Virtual DOM is a copy of the actual DOM, and it is used to make changes to the page without having to directly manipulate the actual DOM.

When a user interacts with a React application, the Virtual DOM is updated with the changes. React then compares the Virtual DOM with the actual DOM and only updates the parts of the page that have changed. This makes the process of updating the page much faster and more efficient than if the entire page had to be re-rendered each time a change was made.

The Virtual DOM is an important part of React because it allows developers to create dynamic user interfaces that are fast and responsive. It also makes it easier to debug and maintain the code, since only the parts of the page that have changed need to be updated.

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

What is the purpose of the React component lifecycle?

A

The React component lifecycle is a series of methods that are called automatically by React during the creation and updating of a component. These methods allow developers to hook into the lifecycle of a component and run code at specific points in the process. The purpose of the React component lifecycle is to provide developers with the ability to control how a component is created, updated, and destroyed.

The React component lifecycle consists of three main phases: initialization, update, and destruction. During the initialization phase, the component is created and the componentWillMount() and componentDidMount() methods are called. During the update phase, the componentWillReceiveProps() and componentWillUpdate() methods are called when the component’s props or state are changed. Finally, during the destruction phase, the componentWillUnmount() method is called when the component is removed from the DOM.

The React component lifecycle provides developers with the ability to control how a component is created, updated, and destroyed. This allows developers to create components that are more efficient and perform better. Additionally, the lifecycle methods provide developers with the ability to perform certain tasks at specific points in the lifecycle, such as setting up subscriptions or making API calls.

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

How do you handle state management in React?

A

State management in React is an important concept to understand and master. There are several ways to handle state management in React, depending on the complexity of the application.

The most basic way to manage state in React is to use the built-in state object. This is a simple way to store and update data within a component. The state object can be accessed and updated using the setState() method. This is a good option for simple applications with limited data.

Another way to manage state in React is to use a state management library such as Redux or MobX. These libraries provide a more robust way to manage state, allowing for more complex data structures and better scalability. They also provide tools for debugging and testing, making it easier to maintain and update the application.

Finally, React also provides the Context API, which allows components to access data from a global store. This is a good option for applications with complex data structures and multiple components that need to access the same data.

No matter which approach you choose, it is important to understand the basics of state management in React and how to use the tools available to you.

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

What is the difference between a class component and a functional component in React?

A

A class component is a type of React component that is written as a JavaScript class. It extends the base React Component class and implements a render() method that returns a React element. Class components allow you to use additional features such as local state, lifecycle methods, and the ability to handle user events.

A functional component is a type of React component that is written as a JavaScript function. It accepts props as an argument and returns a React element. Functional components are simpler than class components and are often used for UI elements that do not require local state or lifecycle methods. They are also easier to test and are more performant than class components.

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

What is the purpose of the React Context API?

A

The React Context API is a way to pass data through the component tree without having to pass props down manually at every level. It allows you to create a “context” that can be accessed by any component in the tree, no matter how deep it is. This makes it easier to manage data that needs to be accessed by multiple components, such as user authentication, theme, or language settings. It also helps to avoid prop-drilling, which is when you have to pass props down multiple levels just to get the data to the component that needs it. The React Context API is a powerful tool for managing data that needs to be shared across multiple components.

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

How do you optimize the performance of a React application?

A

Optimizing the performance of a React application requires a multi-faceted approach.

First, it is important to ensure that the code is written in an efficient manner. This includes writing code that is concise and easy to read, avoiding unnecessary re-renders, and using the latest React features such as React.memo and React.lazy. Additionally, it is important to use the latest version of React and ensure that all dependencies are up to date.

Second, it is important to ensure that the application is properly structured. This includes breaking the application into smaller components, using the correct component lifecycle methods, and using the correct data structure for the application.

Third, it is important to ensure that the application is properly optimized for the browser. This includes minifying and compressing code, using code splitting, and using caching techniques. Additionally, it is important to ensure that the application is properly optimized for mobile devices.

Finally, it is important to ensure that the application is properly monitored and tested. This includes using performance monitoring tools such as React Profiler and React DevTools, and using automated testing tools such as Jest and Enzyme.

By following these steps, it is possible to optimize the performance of a React application and ensure that it runs smoothly and efficiently.

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

What is the purpose of the React Router library?

A

The React Router library is a routing library for React applications that provides a way to declaratively map routes to components. It allows developers to create and manage routes in a React application, allowing for navigation between different components and views. It also provides features such as dynamic route matching, location tracking, and route lifecycle hooks. React Router is designed to make it easy to create and maintain complex routing configurations in a React application. It is also designed to be extensible, allowing developers to customize and extend the library to fit their specific needs.

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

How do you handle asynchronous data fetching in React?

A

Asynchronous data fetching in React can be handled using the React component lifecycle methods. The componentDidMount() method is the most commonly used lifecycle method for fetching data. This method is called after the component is mounted and the initial render is complete. Inside this method, you can make an API call to fetch the data and then set the state with the data received.

Another way to handle asynchronous data fetching in React is to use the useEffect() hook. This hook is called after the component is mounted and the initial render is complete. Inside this hook, you can make an API call to fetch the data and then set the state with the data received.

You can also use the async/await syntax to handle asynchronous data fetching in React. This syntax allows you to write asynchronous code that looks like synchronous code. Inside the async function, you can make an API call to fetch the data and then set the state with the data received.

Finally, you can use the Fetch API to handle asynchronous data fetching in React. The Fetch API is a browser API that allows you to make network requests. Inside the Fetch API, you can make an API call to fetch the data and then set the state with the data received.

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

What is the purpose of the React Hooks API?

A

The purpose of the React Hooks API is to provide a way for React developers to use state and other React features without writing a class. React Hooks allow developers to use state and other React features in functional components, which are components that are written as a function rather than as a class. This makes it easier for developers to write components that are more concise and easier to read. Additionally, React Hooks allow developers to share logic between components, which can help reduce the amount of code that needs to be written. Finally, React Hooks allow developers to use React features without the need to use classes, which can help improve the performance of React applications.

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

How do you debug a React application?

A

Debugging a React application can be done in a few different ways.

The first way is to use the React Developer Tools, which is a browser extension that allows you to inspect the React component hierarchy in the Chrome or Firefox developer tools. This allows you to view the component tree, props, state, and more. It also allows you to trace the component hierarchy and view the component’s source code.

The second way is to use the console.log() method to log out the values of variables and objects. This can be used to debug the application by seeing what values are being passed around and what is causing errors.

The third way is to use the React Error Boundaries feature. This feature allows you to wrap components in an error boundary and catch errors that occur within the component. This can be used to debug the application by seeing what errors are occurring and where they are occurring.

Finally, you can use the React Profiler to profile the performance of your application. This allows you to see how long components are taking to render and which components are taking the longest. This can be used to identify performance bottlenecks and optimize the application.

Overall, debugging a React application can be done in a few different ways, depending on the type of issue you are trying to debug.

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

What is the difference between the virtual DOM and the real DOM?

A

DIFFING!! The Document Object Model (DOM) is a programming interface for HTML and XML documents. It represents the web page as a tree-like structure of nodes and allows developers to manipulate the page’s content and structure using JavaScript.

The Real DOM, also known as the live DOM, is the actual representation of the web page that exists in the browser’s memory. When changes are made to the content or structure of a web page using JavaScript, the Real DOM is updated to reflect those changes.

The Virtual DOM, on the other hand, is an abstract representation of the Real DOM that is used by JavaScript frameworks such as React, Vue, and Angular. Instead of manipulating the Real DOM directly, these frameworks make changes to the Virtual DOM, which is then compared to the Real DOM to determine what changes need to be made.

The main difference between the Virtual DOM and the Real DOM is that the Virtual DOM is much faster to manipulate and update than the Real DOM. When changes are made to the Virtual DOM, the framework updates the Real DOM only with the necessary changes, rather than updating the entire DOM tree. This makes the process of updating the web page much faster and more efficient, which can result in better performance and a smoother user experience.

In summary, the Real DOM is the actual representation of the web page that exists in the browser’s memory, while the Virtual DOM is an abstract representation of the Real DOM used by JavaScript frameworks to make changes to the web page more efficiently.

Virtual DOM cannot directly update an HTML document - more of a pattern than a technology. Synced with the REAL DOM.

object-based representation of an HTML document - copy of the real DOM - can be updated

MOST IMPORTANT!! DIFFING - virtual dom looks at the difference between one stae and the next state and it just updates that individual piece of the DOM.

In the context of web development, “diffing” (short for difference checking) is the process of comparing two versions of a document or data structure to identify the differences between them.

In the case of the Virtual DOM, diffing is used by JavaScript frameworks like React to compare the previous version of the Virtual DOM with the updated version of the Virtual DOM after a change has been made. The framework uses an algorithm to analyze the differences between the two versions and determine the minimum number of changes that need to be made to the Real DOM to update the web page.

This process of comparing the Virtual DOM and updating the Real DOM with the minimum number of changes is much faster and more efficient than updating the entire DOM tree, which can be slow and cause performance issues.

The diffing algorithm used by JavaScript frameworks typically works by comparing the element type, attributes, and children of each node in the Virtual DOM. It then generates a list of changes that need to be made to the Real DOM to update the web page, such as adding, removing, or updating nodes.

By using diffing to update the Real DOM only with the necessary changes, JavaScript frameworks can provide a smoother user experience and improve the performance of web applications.

THE SHADOW DOM - browser specific technology - not a pattern, actually a technology that is for specific types of elements that you don’t need to code, but can use for styling - such as a slider. You won’t necessarily need to create the whole slider.

The Shadow DOM (Document Object Model) is a web standard that enables the creation of encapsulated and isolated DOM trees within an HTML document. It allows developers to define custom HTML elements with their own CSS styles and JavaScript behavior, without interfering with the rest of the page’s styles or behavior.

In other words, the Shadow DOM provides a way to create “scoped” or “local” styles and scripts, which are attached to a specific element and its children, and don’t affect the styles or scripts of other parts of the page. This is particularly useful for building reusable components or widgets, where you want to keep the internal implementation details hidden from the rest of the page.

The Shadow DOM works by creating a separate sub-tree of DOM nodes and elements, which is associated with a host element in the main DOM tree. The content of the Shadow DOM is hidden from the outside world, and can only be accessed and manipulated by scripts running inside the Shadow DOM itself.

Overall, the Shadow DOM is a powerful tool for creating modular and maintainable web applications, and is supported by all modern web browsers.

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

Is React a framework or a library?

A

Library! Framework is like next.js or gatsby

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

Benefits of React?

A

Maintained by Facebook - large company keeping it up to date - but it is also open source can people can contribute.

Great docs

Component-Based Architecture: React follows a component-based architecture, which allows developers to break down the user interface into reusable and independent components. This modular approach makes it easier to build and maintain complex UIs.

Declarative Syntax: React uses a declarative syntax, which means developers describe what the UI should look like based on the current state of the application. This makes the code more readable and easier to understand, as developers don’t have to manually manipulate the DOM.

Rich Ecosystem: React has a large and active community, which has resulted in a rich ecosystem of libraries, tools, and resources. This makes it easier to find solutions to common problems, integrate with other technologies, and enhance the development process.

Virtual DOM: React uses a virtual DOM (Document Object Model) to efficiently update and render components. The virtual DOM is a lightweight copy of the actual DOM, and React uses it to determine the minimal number of updates needed to keep the UI in sync with the underlying data. This approach improves performance and makes React applications faster.

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

What is JSX?

A

Javascript XML

Allows you to write javascript with an HTML-like template syntax (these have elements which represents objects).

JSX (JavaScript XML) is a syntax extension for JavaScript that allows you to write HTML-like code directly in your JavaScript files. It is used primarily in React , but can also be used in other JavaScript libraries or frameworks.

With JSX, you can create elements and components in a more intuitive and expressive way, compared to traditional JavaScript methods like document.createElement() and appendChild(). For example, you can write code like this:

jsxCopy code
const element = <h1>Hello, world!</h1>;

This code creates a new React element that renders an h1 tag with the text “Hello, world!”. The syntax looks similar to HTML, but is actually a special type of syntax that gets transformed into plain JavaScript code by a tool called a “transpiler” (e.g. Babel).

In addition to HTML-like tags, JSX also supports embedding JavaScript expressions using curly braces {}. For example:

jsxCopy code
const name = 'John';
const element = <h1>Hello, {name}!</h1>;

This code creates an element that renders an h1 tag with the text “Hello, John!”. The expression {name} gets evaluated as the value of the name variable.

JSX also allows you to create reusable components by defining functions that return JSX elements. For example:

jsxCopy code
function Greeting(props) {
  return <h1>Hello, {props.name}!</h1>;
}

const element = <Greeting name="John" />;

This code creates a new component called Greeting that accepts a name prop, and renders an h1 tag with the text “Hello, {name}!”. The last line creates an instance of the Greeting component with the name prop set to “John”.

Overall, JSX is a powerful and flexible tool that makes it easier to build complex user interfaces in JavaScript, especially when used in conjunction with a framework like React.

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

What is the difference between an element and a component?

A

An element is created by JSX as an object. A component is a function that returns an element/multiple elements.

In web development, an element and a component are two related but distinct concepts.

An element is a basic building block of a web page, defined by an HTML tag such as <div>, <p>, or <img>. Elements can be styled with CSS, and can have attributes like class, id, or data-*.

A component, on the other hand, is a more complex and self-contained unit of a web application, composed of one or more elements and their associated logic. Components encapsulate a specific functionality or feature of the application, and can be reused across different parts of the application or even across different applications.

In the context of a JavaScript framework like React, components are typically defined as JavaScript classes or functions that return JSX elements. For example:

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

Is it possible to write react code without jsx?

A

You can! with …React.createElement(’div’, null, ‘hellooooo’);

But why would you?

Yes, it is possible to write React code without JSX, although JSX is the recommended and most commonly used way of writing React code.

In fact, JSX is not strictly necessary to use React at all - it is just a syntactic sugar that makes it easier and more intuitive to write React code. Under the hood, JSX code gets transpiled into plain JavaScript code that uses React’s createElement() function to create and configure React elements.

To write React code without JSX, you would need to use the createElement() function directly to create React elements. For example, instead of writing:

jsxCopy code
const element = <h1>Hello, world!</h1>;

you would write:

jsCopy code
const element = React.createElement('h1', null, 'Hello, world!');

This code creates a new React element that represents an h1 tag with the text “Hello, world!”.

You can also use variables and functions to build up more complex React elements. For example:

jsCopy code
const name = 'John';
const element = React.createElement('h1', null, `Hello, ${name}!`);

This code creates a new element that renders an h1 tag with the text “Hello, John!”.

While it is possible to write React code without JSX, it can be more verbose and harder to read and understand. Therefore, JSX is generally considered to be the preferred way of writing React code, especially for larger and more complex applications.

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

Can you pass a prop from a child to a parent?

A

Pass a function prop …. pass/create a function in the child component that passes the variable up to the parent.

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

Can you modify props?

A

no, they are immutable - props are READ ONLY. All react components must act like pure functions

19
Q

What are props?

A

Props are short for properties, and they are passed to a component from its parent component. Props are read-only and cannot be modified by the component that receives them. Instead, they are used to configure a component and to provide it with the data it needs to render its UI. Props are typically used to pass data and callbacks down the component tree, from parent components to child components.

For example, a Button component might receive props such as label, onClick, and disabled, which tell it what text to display on the button, what function to call when the button is clicked, and whether the button should be disabled.

20
Q

What is state?

A

State is data that is managed by a component itself. Unlike props, state can be modified by the component using the setState() method. State is used to keep track of changing data within a component, such as user input, network responses, or internal component state. When state changes, React re-renders the component to reflect the new state.

For example, a Counter component might use state to keep track of the current count value, and update it each time the user clicks a button:

jsxCopy code
function Counter() {
  const [count, setCount] = useState(0);

  function handleClick() {
    setCount(count + 1);
  }

  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={handleClick}>Increment</button>
    </div>
  );
}

In this example, the Counter component uses the useState() hook to define a count state variable and a setCount function to update it. The handleClick function uses setCount to increment the count by 1 each time the button is clicked. The current count value is displayed using the count state variable in the component’s UI.

To summarize, props are used to pass data from parent components to child components, while state is used to manage data within a component itself. Props are read-only and cannot be modified by the component, while state can be modified using the setState() method.

21
Q

What are some ways to pass props to child components?

A

1) The most common way to pass props in React is by passing them as attributes to child components. For example:

jsxCopy code
function Parent() {
  return <Child name="Alice" age={25} />;
}

function Child(props) {
  return <div>{props.name} is {props.age} years old</div>;
}

In this example, the Parent component is passing the name and age props to the Child component, which uses them to render a message.

2) 1. Using spread operator: You can also use the spread operator (...) to pass all the props of an object as individual props to a child component. For example:

jsxCopy code
function Parent() {
  const props = { name: 'Bob', age: 30 };
  return <Child {...props} />;
}

function Child(props) {
  return <div>{props.name} is {props.age} years old</div>;
}

In this example, the Parent component creates an object with the name and age props, and passes them to the Child component using the spread operator.

3) 1. Passing props to DOM elements: You can also pass props to standard DOM elements in a similar way. For example:

jsxCopy code
function Parent() {
  return <div className="container"><h1>Hello, world!</h1></div>;
}

In this example, the Parent component is passing the className prop to the div element, which adds a CSS class to the element.

4) Using props.children: Finally, you can pass children to a component using the special props.children prop. This allows you to pass arbitrary content to a component, which can be accessed and rendered using the props.children property. For example:

jsxCopy code
function Parent() {
  return <Child>Hello, world!</Child>;
}

function Child(props) {
  return <div>{props.children}</div>;
}

In this example, the Parent component is passing the string “Hello, world!” as the child of the Child component, which renders it inside a div element.

These are just a few examples of how to pass props in React - there are many other ways to do so depending on your specific use case.

5) Yes, props can also be passed and destructured using object destructuring syntax in React. This allows you to extract specific props from the props object and use them as separate variables within the component.

Here is an example:

jsxCopy code
function Person({ name, age }) {
  return (
    <div>
      <h2>{name}</h2>
      <p>{age} years old</p>
    </div>
  );
}

function App() {
  const person = { name: 'Alice', age: 25 };

  return <Person {...person} />;
}

In this example, the Person component uses object destructuring syntax to extract the name and age props from the props object. These props are then used as separate variables within the component.

The App component creates an object with the name and age props, and passes them to the Person component using the spread operator and object destructuring.

Note that destructuring props can make your code more concise and readable, especially when passing many props to a component. However, it can also make it harder to see which props are being used, so use it judiciously.

22
Q

What is a problem with destructuring props when passing them to child components?

A

Destructuring props can make your code more concise and readable, especially when passing many props to a component. However, it can also make it harder to see which props are being used, so use it judiciously.

23
Q

What is prop drilling?

A

Continually passing props down the hierarchy causing every component to re-render even if they don’t need that prop.

24
Q

What is the difference between props and state?

A

both are javascript objects. Bot hold info about what is going to be rendered. State is managed within a component… they are scoped to the component. Cannot access it outside of a components - whereas props can be passed.

In React, props and state are both used to manage data in a component, but they have different purposes and are used in different ways.

Props are short for properties, and they are passed to a component from its parent component. Props are read-only and cannot be modified by the component that receives them. Instead, they are used to configure a component and to provide it with the data it needs to render its UI. Props are typically used to pass data and callbacks down the component tree, from parent components to child components.

For example, a Button component might receive props such as label, onClick, and disabled, which tell it what text to display on the button, what function to call when the button is clicked, and whether the button should be disabled.

State is data that is managed by a component itself. Unlike props, state can be modified by the component using the setState() method. State is used to keep track of changing data within a component, such as user input, network responses, or internal component state. When state changes, React re-renders the component to reflect the new state.

For example, a Counter component might use state to keep track of the current count value, and update it each time the user clicks a button:

jsxCopy code
function Counter() {
  const [count, setCount] = useState(0);

  function handleClick() {
    setCount(count + 1);
  }

  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={handleClick}>Increment</button>
    </div>
  );
}

In this example, the Counter component uses the useState() hook to define a count state variable and a setCount function to update it. The handleClick function uses setCount to increment the count by 1 each time the button is clicked. The current count value is displayed using the count state variable in the component’s UI.

To summarize, props are used to pass data from parent components to child components, while state is used to manage data within a component itself. Props are read-only and cannot be modified by the component, while state can be modified using the setState() method.

25
Q

What is state in a class vs a function component?

A

In React, state is a concept that represents the data that a component manages and can change over time. It allows components to keep track of information and update the UI based on that information.

In a class component, state is managed using the this.state property. The state is initialized in the constructor of the class component using this.state = { /* initial state values */ }. To update the state, you use the this.setState() method, which merges the new state values with the existing state.

Here’s an example of managing state in a class component:

class Counter extends React.Component {
constructor(props) {
super(props);
this.state = {
count: 0
};
}

incrementCount() {
this.setState({ count: this.state.count + 1 });
}

render() {
return (
<div>
<p>Count: {this.state.count}</p>
<button onClick={() => this.incrementCount()}>Increment</button>
</div>
);
}
}
In a function component, state can be managed using the useState hook, which is a built-in hook provided by React. The useState hook returns an array with two elements: the current state value and a function to update the state.

Here’s an example of managing state in a function component using the useState hook:

import React, { useState } from ‘react’;

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

const incrementCount = () => {
setCount(count + 1);
};

return (
<div>
<p>Count: {count}</p>
<button onClick={incrementCount}>Increment</button>
</div>
);
}
In the function component example, the useState hook is used to declare a state variable count and a function setCount to update the state. The initial state value is provided as an argument to the useState hook.

Both class components and function components can manage state, but the syntax and approach differ. Class components use the this.state property and this.setState() method, while function components use the useState hook.

It’s important to note that with the introduction of React Hooks in React 16.8, function components have become more powerful and can handle state and other lifecycle functionalities previously only available in class components.

26
Q

What are the 3 main parts of the component lifecycle?

A

3 main parts - mounting, updating, unmounting.

1)render, componentDidMount,

2) render, componentDidUpdate, - functions that will run whenever the component did update because state has changed

3) componentWillUnmount - when there is an event listener - remove that event listerner when it is unmounting and removed from the page.

use with class based components - but just stick to function components.

27
Q

How do you update lifecycle in function components?

A

useEFFECT! In React function components, you can use the useEffect hook to update the lifecycle. The useEffect hook is a function that takes a callback function as its first argument and an optional array of dependencies as its second argument.

Here’s an example of how to update the lifecycle in a function component using the useEffect hook:

jsxCopy code
import React, { useState, useEffect } from 'react';

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

  // Component did mount and did update
  useEffect(() => {
    // This code will run after the first render and after every update
    console.log(`Count is now ${count}`);
  });

  // Component did mount only
  useEffect(() => {
    // This code will only run after the first render
    console.log('Component mounted');
  }, []);

  // Component did mount and when count changes
  useEffect(() => {
    // This code will run after the first render and whenever count changes
    console.log(`Count changed to ${count}`);
  }, [count]);

  function handleClick() {
    setCount(count + 1);
  }

  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={handleClick}>Increment</button>
    </div>
  );
}

In this example, we’re using the useEffect hook to log messages to the console at different stages of the component’s lifecycle. The first useEffect callback will run after the initial render and after every subsequent update. The second useEffect callback will only run after the initial render. The third useEffect callback will run after the initial render and whenever the count state changes.

The useEffect hook takes care of managing the lifecycle of the component, including handling updates and unmounting. By using the useEffect hook, you can add lifecycle methods to function components in a similar way to class components, but with less boilerplate code.

28
Q

What is “state”?

A

All the internal data that defines an application at a given point in time

29
Q

What is React’s approach to reactive UI rendering?

A

You declare how state is represented as visual elements of the DOM. From then on, React automatically updates the DOM to reflect state changes.

30
Q

What is React’s ‘raison d’etre’?

A

Reduce the complexity of creating/maintaining UIs

31
Q

What are components?

A

Self-contained, concern-specific building blocks that are easy to reuse, extend and maintain.

32
Q

Give 3 reasons why JSX is great for describing user interfaces?

A

1) it’s declarative
2) it’s easy to spot the relationship between elements
3) it’s easy to visualize the overall structure of your UI.

33
Q

How did the web “used” to work?

A

For every interaction the user performed on a page, a whole new page was sent from the server (even if this new page was only a slightly different version of the page the user was on).

34
Q

What are SPAs constantly doing? (single page applications)

A

Fetching new data
transforming parts of the DOM as the user interacts through the UI

35
Q

What happens when interfaces grow more complex?

A

It gets more difficult to:

examine the current state of the application
make the necessary punctual changes on the DOM to update it

36
Q

What technique do other JS frameworks use to keep the interface in sync with state?

A

Data binding

37
Q

For performance reasons it’s not viable to trash and re-render the entire interface every time state changes. What’s React’s solution to this?

A

The ‘virtual DOM’ - an in-memory, lightweight representation of the DOM

38
Q

What is the advantage of the virtual DOM over the real DOM?

A

It’s faster to manipulate than the real DOM

39
Q

What are two typical things that might change the state of an application?

A

User interaction and data fetching

40
Q

How does React use the virtual DOM?

A

It quickly compares the current state of the UI with the desired state.
It then computes the minimal set of real DOM mutations to achieve the change.

41
Q

What is the ‘end benefit’ of React’s virtual DOM?

A

It makes React very fast and efficient.
React apps can easily run at 60 fps, even on mobile devices

42
Q

What is the advantage of being able to combine components?

A

We can create more complex and feature-rich components

43
Q

React components are written in plain JS. What has traditionally been used for web application UIs though?

A

Templating languages and HTML directives

44
Q

Are server-side components of the next.js a new concept? 😒

A

Traditionally, web pages were built on the server side, which meant that the HTML, CSS, and JavaScript for each page was rendered on the server and sent to the client. This made it easy for search engines to index web pages, as they could simply crawl the HTML that was served to them.

However, with the rise of JavaScript frameworks like React, web pages began to be built on the client side. This meant that the HTML, CSS, and JavaScript for each page was rendered in the browser after the page was loaded. This made it more difficult for search engines to index web pages, as they could only see the initial HTML that was served to them.

To address this issue, React server components were created. These components allow the HTML, CSS, and JavaScript for a React page to be rendered on the server before the page is sent to the client. This means that search engines can index the rendered HTML, which can improve the SEO of React pages.

Next.js is a React framework that makes it easy to use React server components. It provides a number of features that make it well-suited for SEO, such as pre-rendering pages, generating static HTML, and handling redirects.

In summary, server-side rendering is not a new thing it has been used in other frameworks a while ago like in Django, and Flask, but it has become more important with the rise of JavaScript frameworks. React server components and frameworks like Next.js can help to improve the SEO of React pages.