Enter Your Email Address to Watch This Lesson

Your link to unlock this lesson will be sent to this email address.

Unlock this lesson and all 1023 of the free egghead.io lessons, plus get TypeScript content delivered directly to your inbox!



Existing egghead members will not see this. Sign in.

Create stateful React Components using TypeScript

2:56 TypeScript lesson by

You can create stateful React Components in TypeScript by extending from the React.Component class. This parent class takes two generic parameters, Props and State.

This lessons shows these generic parameters and React.Component in action

Get the Code Now
click to level up

egghead.io comment guidelines

Avatar
egghead.io

You can create stateful React Components in TypeScript by extending from the React.Component class. This parent class takes two generic parameters, Props and State.

This lessons shows these generic parameters and React.Component in action

Avatar
John

Out of curiosity, @1:45. What are those two letters appearing on everything?

Avatar
Basarat Ali Syed

Its called jumpy and comes with http://alm.tools/

Allows you to jump at a particular point in the file quickly 🌹

In reply to John

Here, I have a simple application that renders the div "Hello World" to the DOM using React and ReactDOM.

We can easily move this div into a stateful component called app by extending from React's component and returning the div from the render function of this new component. One big advantage of using components is that you get to use props to change the component behavior.

React.Component takes two generic parameters. The first one is the prop. We can go ahead and add a prop message of type string. We can use this prop in our render method.

As you can see, TypeScript is already complaining on the misusages of the component. It is saying that the component expects a prop message to be passed in. Let's go ahead and pass it in. You can see that this property is now being used within the render method.

Components that extend from React.Component are called "stateful," because they get to have their own internal state. The second generic parameter that React.Component takes is actually the type of the state.

Let's go ahead and set up our state to have account member of type number. We can initialize the state in our constructor. When adding a constructor to a React component, you get passed initial props, which we simply pass to the super React.Component class.

Next, we set up our initial state, which thanks to our generic setup, is just with autocomplete. We go ahead and set up the count member to be zero initially.

Finally, we get to use state in other places like the render function. Go ahead and display this.state.count initially. The key reason for having local state in a component is of course that you get to manage it inside the component.

For example, we can go ahead and add an increment function that uses React components, set state to increment the count member of the state.

Next, within our render function, we can go ahead to our root div and add an on-click handler to call this increment function.

Now, if we go ahead and click the root div rendered by the component, you can see that the state changes correctly, causing the component to re-render with a new state.

Let's say we want to add another property, foo, to our component state. As soon as we do that, you will notice that all calls to set state will be marked as an error if they do not pass in this additional member, foo, for state.

We can fix this easily by marking this new foo member as an optional member. With that, the errors go away. In fact, because of how React setState works, it is conventional to mark all state members as optional.

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?