class Components in React?

Question
  • A class component requires you to extend from React.Component and create a render function which returns a React element.
  • all lifecycle hooks are coming from the React.Component which you extend from in class components. So if you need lifecycle hooks you should probably use a class component. The class component is also known as a stateful component because it can hold or manage the local state. You can pass data from one class to another class components. You can create a class by defining a class that extends Component and has a render function.
  • The valid class component is shown in the below example.

class Welcome extends React.Component {
render() {
return <h1>Hello, {this.props.name}</h1>;
}
}

3 years 2021-08-05T04:33:51+00:00 3 views 0

Leave an answer

Browse
Browse