Simple State Management with RxJS — a To-do List example

What is RxJS ?

RxJS is a Reactive Extensions Library for JavaScript which is getting very popular.

According to Wikipedia, Reactive Programming is a declarative programming paradigm concerned with data streams and the propagation of change….


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Duc Le

What is RxJS ?

RxJS is a Reactive Extensions Library for JavaScript which is getting very popular.

According to Wikipedia, Reactive Programming is a declarative programming paradigm concerned with data streams and the propagation of change.

I will cover some basics before moving on:

  • Observable: represents the idea of an invokable collection of future values or events.
  • Observer: is a collection of callbacks that knows how to listen to values delivered by the Observable.
  • Subscription: represents the execution of an Observable, is primarily useful for cancelling the execution.

Setting up the project

You can use basically any UI Framework or library out there, even with vanilla JS. In this article, I will go with SolidJS ( you can find it very similar to React )

npx degit solidjs/templates/ts my-app

This will generate a new Solid app with TypeScript, next, just add the RxJS library

yarn add rxjs
yarn dev

Setup store

In the src folder, let’s create the state.ts file ( you can name it store or whatever ) then create the initialState and subject

Image description

Subscriptions

An RxJS Subject is the only way of multicasting a value or event to multiple Observers. In this example, we’ll be subscribing our different setter functions to our RxJS Subject so that when it receives any data, it forwards that data to every state associated with our setter function ( if you familiar with React, then just pass a setState to it )

Then, we will subscribe to it

Image description

Next, we will create an init method to initialize the state

Image description

The next method is used to push a value to the subject, when we call the next method with a value as its parameter, that value is casted to all Observers subscribed to the Subject.

Add, delete and modify the state

Nothing much to say about this, just like every other to-do list example, we need multiple method to modify the state that holds the tasks

Image description

This is a complete version of our app, you can add more methods if you want to fulfill you desire. The final product might looks like this, depend on your styling :

Image description

Hope this tutorial can help you understand basics of RxJS and its usage. You can find the source code here

References:

https://rxjs.dev/
https://en.wikipedia.org/wiki/Reactive_programming
https://blog.logrocket.com/rxjs-react-hooks-for-state-management/


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Duc Le


Print Share Comment Cite Upload Translate Updates
APA

Duc Le | Sciencx (2022-12-06T04:34:27+00:00) Simple State Management with RxJS — a To-do List example. Retrieved from https://www.scien.cx/2022/12/06/simple-state-management-with-rxjs-a-to-do-list-example/

MLA
" » Simple State Management with RxJS — a To-do List example." Duc Le | Sciencx - Tuesday December 6, 2022, https://www.scien.cx/2022/12/06/simple-state-management-with-rxjs-a-to-do-list-example/
HARVARD
Duc Le | Sciencx Tuesday December 6, 2022 » Simple State Management with RxJS — a To-do List example., viewed ,<https://www.scien.cx/2022/12/06/simple-state-management-with-rxjs-a-to-do-list-example/>
VANCOUVER
Duc Le | Sciencx - » Simple State Management with RxJS — a To-do List example. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/12/06/simple-state-management-with-rxjs-a-to-do-list-example/
CHICAGO
" » Simple State Management with RxJS — a To-do List example." Duc Le | Sciencx - Accessed . https://www.scien.cx/2022/12/06/simple-state-management-with-rxjs-a-to-do-list-example/
IEEE
" » Simple State Management with RxJS — a To-do List example." Duc Le | Sciencx [Online]. Available: https://www.scien.cx/2022/12/06/simple-state-management-with-rxjs-a-to-do-list-example/. [Accessed: ]
rf:citation
» Simple State Management with RxJS — a To-do List example | Duc Le | Sciencx | https://www.scien.cx/2022/12/06/simple-state-management-with-rxjs-a-to-do-list-example/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.