Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Create stateful React Components using TypeScript

    Basarat Ali SyedBasarat Ali Syed

    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



    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson




    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.