Sobes.tech
Middle

What are the two main types of components in React?

sobes.tech AI

Answer from AI

In React, there are two main types of components:

  1. Functional Components — these are functions that accept props and return JSX. They are simpler and lighter, especially with the introduction of hooks, which allow using state and other React features.

  2. Class Components — these are ES6 classes that extend React.Component and have lifecycle methods, state, and other capabilities.

Example of a functional component:

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

Example of a class component:

class Greeting extends React.Component {
  render() {
    return <h1>Hello, {this.props.name}!</h1>;
  }
}
What are the two main types of components in React… - sobes.tech