Redux is a powerful state management library primarily used in JavaScript applications, particularly those built with frameworks like React. At its core, Redux provides a predictable state container for managing the state of an application in a more organised and centralised manner. It operates on a unidirectional data flow model, which helps in maintaining the consistency of application state and facilitates easier debugging and testing.
Redux is used for several reasons:
A Redux Store is the global state tree of the application in which all the global states of the app are stored, check out more about Redux Stores.
The Redux Actions are the operations (events) to be performed on the store. A set of update changes, Know more about Redux Actions.
Redux Reducers are functions that take a state and an action as arguments and returns a new state which will be overriding the previous one.
In a Counter App, the store will have a counter state (with number value), an action incrementing/decrementing the value, two reducers to handle the actions and return a new state for each action.
For this project, we'll be using:
Let's install all the dependencies starting with the new Next project:
I'll be using TypeScript and tailwind css, you can config that while executing the previous command. Now, let's install the redux dependencies:
The redux toolkit will help us setup the redux elements with less boilerplate code. You can know more about the Redux Toolkit here.
In a folder called "redux", create a file under the name "store.ts":
The dispatch a function of the Redux store which we call to dispatch an action and eventually trigger a state change.
Now, we create a slice which is a collection of reducer logic and actions for a single feature in your app, in our redux/userSlice.ts file will define the user feature we need to create, starting with a simple typescript interface shaping the structure of the user state:
Then we need to define the initial value of our state:
Now it's time to create our slice, in which will define our actions and reducers:
We need to export the actions and the slice reducer:
Next, we add our slice reducer to the store, the store.ts file should look like this:
Since, React cannot communicate directly with the Redux store, we need to add the Redux provider at the root our app:
For the UI, will be having a simple user edit and profile sections. Let's create a user profile component, in the components folder, create a file called UserProfile.tsx with the following code:
Next let's create the UserEdit.tsx component:
All we need now is to add the two components to the main page app/page.tsx, let's create a components/index.ts file:
Our page.tsx should look like this:
When we run our app using the following command:
We should be a having a similar page on the http://localhost:3000:
As you saw, the operations we performed are synchronous, in real-world use cases we must deal with async operations such as Api calls and IO processes. Setting up an async action is the next step, it will focus on the "unverification", we turn the user status from verified to unverified. In the userSlice.ts file, we create and export the action:
Then in the userSlice const, we can add the async action using the extraReducers property.
In the UserEdit.tsx, we need to add another button which will dispatch the async action we just created:
Also on the UserProfile.tsx, we need to make use of the user.isLoading property:
After the new tweaks, we should be having a similar page:
As a personal opinion, I believe Redux is huge help when it comes to state management, especially in large and complex projects, this article only serves as a simple getting-started PoC :))
Please, find the source code repository here
Blossoming Intelligence: How to Run Spring AI Locally with Ollama
In this short article, we'll look at how easy it is to create a chat bot backend powered by Spring and Olama using the llama 3 model.
Sat, 11th May 2024
Read MoreGetting started with native java apps with GraalVM
Native Image is a technology to ahead-of-time compile Java code to a standalone executable, called a native image. This executable includes the application classes, classes from its dependencies, runtime library classes, and statically linked native code from JDK.
Wed, 10th April 2024
Read MoreLeveraging Spring Reactive, Functional Endpoints, Docker, and MongoDB
Blocking is a feature of classic servlet-based web frameworks like Spring MVC. Introduced in Spring 5, Spring WebFlux is a reactive framework that operates on servers like Netty and is completely non-blocking. Two programming paradigms are supported by Spring WebFlux. Annotations (Aspect Oriented Programming) and WebFlux.fn (Functional Programming).
Thu, 29th February 2024
Read MoreMonitor Spring reactive microservices with Prometheus and Grafana: a how-to guide
Micro-services monitoring is a crucial aspect of managing modern, complex software architectures. Unlike traditional monolithic applications, micro-services break down functionality into smaller, independent services that can be developed, deployed, and scaled independently.
Fri, 27th October 2023
Read MoreHands on Reactive Spring with Redis Cache and Docker support
The concept of reactive programming enables more responsive and scalable programmes by handling asynchronous data streams.
Mon, 28th August 2023
Read More