Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is redux for if I use a database for my react app?

So recently I started to implement a database in a react app (like any real world app would have). My question is Why is redux needed? I mean if you have a database you can push information directly in a database and retreive it from there. You don't need to save it in a state. (New to react,redux. This was just my point of view)

like image 246
Roland Avatar asked Jan 16 '19 08:01

Roland


1 Answers

Database and state management tools such as Redux have different concerns (although they manipulate the same thing: data).

When a client uses your app, it will first retrieve data from the database. At that point, that data needs to be held in memory in order to display it.

You can decide to use the React Component internal state which is component-scoped. Now, this is perfectly fine if you plan on using the data you just retrieved inside the same component.

As your app gets more complex, you'll sometimes need to use the data in different points throughout your app (e.g. if you retrieve the user info, you'll probably need to display it in the header, in the profile page, etc).

This can be tricky to do using the React Component internal state as (if you've tried React a bit) you know passing data is done by passing props down children components.

The common solution when you need to share data between different components is to lift the state up in your app, so you can pass it down to the different components that need it.

This can be tedious and prone to errors as your app grows.

Redux is the solution that addresses that concern. It helps keeping the state you share throughout your app clear and clean by creating a global state that can be accessible anywhere in your application (among other things).

like image 175
Saraband Avatar answered Oct 16 '22 20:10

Saraband