Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
1×
Become a member
to unlock all features

Level Up!

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

Autoplay

    Creating Reusable State Class Driven React Components

    Basarat Ali SyedBasarat Ali Syed
    reactReact
    mobxMobX
    typescriptTypeScript

    The state class pattern can easily be expanded to allow you to create component-ui / component-state pairs that provide reuseable functionality for your applications.

    In this lesson we look at an example of creating a reuseable input component.

    We also mention a re-useable form state management library that uses the state class pattern.

    Code

    Code

    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
    Discuss

    Discuss

    Transcript

    Transcript

    Instructor: We start off with the simple list application from the previous lesson. You can add items to the list and reset the list if you want. Now, looking at the app, it could be great if you had a reusable input component.

    Similarly, if you look at application state, it could be great if you could create a reusable field state instead of having to create two things. You create a new file field.tsx to contain these two reusable pieces of information.

    You bring in the usual React observable and action from MobX and observer from Mobx React. We can refactor the reusable portion of the state into a nice class called field state that has the value and the onChange action to change the value.

    Similarly, we can refactor the rendering of the field state into a reusable field input component that takes the field state as a prop and wires it into a browser native input element. In our application state, instead of these two pieces of information, we have a single field state. Now, we simply carry out the refactoring required as courteously pointed out by TypeScript.

    This already results in a few less lines and less lines of isolatable logic into our application code. Similarly, in app.tsx, we get to use our field input and simply wire it to the current item in the application state.

    As you can see, the application still works as expected. Note that, if you wanted, we could easily share this reusable logic of field state maintenance. Next ideas to include in such a class would be adding validation and error maintenance along with the ability to compose field states into a nice cohesive form state.

    This is exactly what the library form state does. You bring it in from npm and just use that instead of our custom rolled field state. We wire our input to use that generic field state and initialize the current item in app state to also use the same field state.

    Note that, having the ability to author our own field input allows us to design an input that matches our requirements. We get to define our own design without having to worry about the core logic of field state management and validation.

    There is, of course much more to the form state library. However, the focus here was simply to demonstrate the reusability of state classes with state class prop components.