QrQr
QrQr

Reputation: 181

When to use redux in React Native?

I'm trying to build an app, it has 3 Reducers

  1. AccountDetails it holds all kind of user's products
  2. GeneralData (eg. device's unique id, token, etc)
  3. shopping carts

Me and my coworker currently building a registration screen that has 6 screens (the ui/ux team request this)

  1. input username
  2. input password & confirm the password
  3. input DoB
  4. input Email
  5. screen to submit all the four data nb: we use react-navigation

so my coworker suggesting to use redux to handle this kind of data flow, I think it is kinda "overkill" to create another redux for register, why not passing the username data, password, DoB, and email to route.params and submit it to the last screen and we will have lot of Reducers to handle registration, recover password, recover username, and maybe other things.

but we both want to make this application scalable and easier to maintain, but i don't really know the proper way to do that

Upvotes: 1

Views: 496

Answers (3)

pullidea-dev
pullidea-dev

Reputation: 1823

Your project is simple that has only 6 screens. In case of simple project, you don't have to use Redux. Whether to use Redux or not depends on your decision. But as your project expands, you'll find it extremely difficult to handle all the states with route.params. Then you'll need to use something special to handle the states effectively and that must be Redux. Redux is not something special but centralized state management system.

Upvotes: 2

Maxwell
Maxwell

Reputation: 546

The general way to think about answering 'Should I use Redux for this?" is whether or not that piece of information will be used in other places throughout the app.

So for your scenarios you've got:

  • Account Details -> 100% use Redux
  • General Details -> Maybe. Usually there's simpler ways for tokens and such like just putting them in LocalStorage or AsyncStorage
  • Shopping Cart -> Probably. Again just ask yourself if you'll be using that information in more than a couple screens that are close together

As per your debate with your coworker, I'd say the answer is a combination of both. Passing that data inbetween screens is perfectly fine (and using Redux each step of the way is certainly overkill), but you'll want to add all those values to Redux at the end of a successful sign up. Cause those are the 'Account Detail's' you spoke of earlier are they not?

Upvotes: 1

Andrew Hulterstrom
Andrew Hulterstrom

Reputation: 1725

A lot of people take the approach of wanting to throw Redux at every situation. A lot of other people try to avoid Redux, using it only when absolutely necessary.

You mentioned that you and your coworker both want to make the application scalable and easier to maintain. You can make an argument for doing it either of the two ways you are considering, so neither way is necessary wrong.

However, the general consensus is that you should probably not put your form state in Redux. Redux has an FAQ page on this question here.

Upvotes: 1

Related Questions