learnreact.com is moving to school.reactpatterns.com. Get 50% off while we move.

Learn React

Learn React | <!-- -->Essetial React<!-- --> > <!-- -->Class Components

Essetial React

Class Components

Component functions are fairly limited in their functionality. They take arguments and then render out some type of React element using those arguments. We can also define component with a class. While under the hood these class components are also still functions, they give our components a lot of special powers that we will learn about through the rest of this course. Let’s define this greeting component as a class. We’re going to extend here React’s base class, which is just Component. Once we’ve done that, every component needs to have a render method, and that will be exactly like we did in our function version. We’re going to return an h1 with React {props.name} and h1. Now we run into a little something here where don’t actually get props as an argument. Those are going to be on the class, so we’ll use this. But apart from that, it works exactly like our function version. Now, that component’s mostly done, but I do have some other things in mind. I’d like to make a clap counter next. Let’s define that now. We have a class called ClapCounter. It extends the base React component, and that has to have a render method, which returns a DIV that says ClapCounter, just so we know it’s there.



Keep in touch!

Get notified of new React content and resources

    We respect your privacy. Unsubscribe at any time.