I believe in any application there should be one source of truth.
My app will be having
So in React I found three confusing places to store state:
Redux form does not registers hidden fields. To register hidden fields you have to create a field with disabled attribute, etc.
If you do any computation like, AMOUNT = QTY * RATE; Here user will input QTY and RATE And AMOUNT will be computed. Here it will immediately reflect in component state, but not in redux-form state. To make it reflect in redux-form we have to fire.
this.props.dispatch(change('Invoice', 'amount', 55))
It won't be always possible to avoid component state, if I write computation formula code will look like this
Only Redux-form state
const amount = someReduxFormApiGet(QTY) + someReduxFormApiGet(RATE)
this.props.dispatch(change('Invoice', 'amount', 55))
Only react state
onChange(){ will have set QTY & RATE in component state}
const amount = this.state.QTY * this.state.RATE
Conclusion: If I go with redux-form
I will have to write extra code to make redux-store stable in sync, where as state in the React component I will be having the handleChange()
function that will be drawing state in this.state
. Also feel I will be having lots of flexibility in component state
If my data model becomes more complex then it will be very much difficult to manage in redux-form store. Then here I am thinking to not use redux Custom store OR Component state
Other libraries that validate React inputs are not using Redux to implement validation. It's just redux-form that is using redux to manage validation.
So I reached the conclusion that
Redux-form was born just for validation and not for managing a complex data model.
Complex data models should be managed in redux custom store OR component state accordingly and not Redux-form
From the documentation of Redux-form it handles validation very well, but for data modelling purposes it is suggesting solutions that are not 100% straightforward
Need Help In Deciding
Should I use redux-form store for data modelling
OR
just use for validation
AND
use Component state & custom redux store to model data?
Redux-form is a really great library for working with validations. You can simply develop a lot of validations for different situations. Hence it provides validation functions to validate all the values in your form at once. You may also provide individual value validation functions for each Field or FieldArray.
Some users prefer to keep every single piece of data in Redux, to maintain a fully serializable and controlled version of their application at all times. Others prefer to keep non-critical or UI state, such as “is this dropdown currently open”, inside a component's internal state. Using local component state is fine.
Your approach to use both redux store and local store is correct. Just do not try to duplicate the state from redux store in your component.
Thanks to erikas for the beautiful library. I have been using since it was born nearly two years ago.
I personally don't use redux-form for data manipulation. I do it by self in component state by onChange setting state. When I came to know data store in redux-form, I initially used it, but as my use cases grew, I have to shift data store to component state. Data modelling in react is something that you need to learn, it won't come by magic. But once you do it you will never get confused about Overrated state management in React
If you are performing Data modelling, dependent computations
I will recommend using component state for data.
Thumb Rules:
Component state
then don't use redux-form
state (recommended)redux-form state
then don't use Component state
(limitation - point 1,2,3 and code dirty magic behind the scene managing store)redux custom store
as toggle, user sign-in global data
flavor. But not for storing actual transactional form state (limitation -will do over coding).If your app is going to be complex I recommend you to use
component
state - actual form state with onChange
redux
- use it for global in mngt(toggle,post drafts, users login info only only)
redux-from
- for validation only and not as data store for submition
I also discourage you from keeping heavy, complex, state, and transactional form state in redux custom store
. Just use it like ice-cream toppings.
Pros: Component state - Full control over data, flexibility what comes in what goes out, no magic behind the scenes
Cons: I didn't found any
Conclusion: redux-form
is basically for very newbies
to get things done quickly, but when it comes to computations, dependent fields, commercial data modelling,redux-form
is not the option. But even if you do use redux-form
for data manipulation soon you will be ending up with using (component state
which cannot be ignored + redux-form state
+ redux custom state
) scattered all over with confusion.
Note : I liked @vijays answer react-chopper library below , it fits all your computation requirements.Also its better than 100% better than mobx and other libraries as they dirty the code
But This react-chopper Examples is quiet empressive
It feels 100% like angular.But its uni flow from inside
Disclaimer: react-chopper is still in development And could be used for only simple to medium usecases.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With