Coupling a Stencil TODO app with Redux

In the previous tutorial, we used Stencil to create a TODO application.
The state of the application was built upon a Parent-Children System.
A Redux Store can be implemented to make our life easier and that’s our new goal today. If you are not familiar with Redux, I’d advise you to check this tutorial, otherwise you will be quite lost.

Redux Store Manager

We start by grabbing the Redux library:

Every changes will happen on the same Redux Store. The TodoList and Todo Components will trigger actions on this store.

Both of them will use the following custom StoreManager Service:

As usual, we have the traditional create, delete and update methods.
The only originality here is the setStore method. We will use this one to pass the shared Redux store later.

Ok, we now have our StoreManager.

TodoList Component

We can now go to the TodoList Component:

Two new imports here:

  1. StoreManager: No surprise
  2. createStore: The function that will create our Redux Store

We have one new store State that we will use later.

Now the tricky part.
The same StoreManager must be shared between components, Dependency Injection isn’t (yet?) implemented in Stencil.
We can import the Class, however, if we use it has it is, it will just create a new StoreManager every time.
I found a very interesting issue on Dependency Injection in Stencil. The connect parameter allows us to link to the component that’s using the “store-manager” tag.

Thanks to this, we can do that:

We use the componentOnReady method to listen for the StoreManager Component creation.
When it’s created, we ask for the instance. We are doing approximately the same in the Todo component:

We just have one more line with the setStore method because the TodoList Component will be the one that will create the Redux store.

We can now have a look at the TodoList componentWillLoad hook:

The getStoreService method is used.
We create some predefined data for our Redux Store and instantiate it. The todos State will be initiated with the Redux store’s initial state.
Finally, we subscribe to the store. When the Redux store will change, the todos State will be updated with the new Redux state.

Let’s not skip the todoReducer. This is the same mechanism we used in the previous  Stencil tutorial:

We can now use the StoreManager:

And update our render method:

We got some new syntax here.
Instead of using an if, the ternary expression is used.
If we are not careful enough, the render method will try to use the todos State while it’s still undefined and an error will be shown in the console until the todos State is initiated. To be clean, we will show an empty <div> when the todos State is not ready.

We can now head to the Todo Component!

Todo Component

And we start with some clean up:

The Event and EventEmitter aren’t required anymore because we will ask the StoreManager to update the Redux Store.
Similarly to the TodoList Component, we acquire the StoreManager by using the getStoreService in the componentWillLoad hook.

We can update our old methods:

We don’t emit events anymore, all we do now is passing the order to the storeManager.

And Voila!
We have our TODO back as it should:
stencil todo final result

BONUS CONTENT
Enter your email address in the box below to get access to this tutorial (and others) source code.

Conclusion

Redux makes our life easier.
Using multiple Event Emitters and Listeners in a big application can quickly lead to a mess. At the moment, Dependency Injection with connect is quite tricky. We can’t use it with the Redux library as it is and we need a StoreManager component, which brings up a new point. The current and upcoming libraries might need some reworks in order to be Stencil-compliant.

No Comments Yet

Leave a Reply

Your email address will not be published. Required fields are marked *

0 Shares