0

In React Native(and so React), using Redux, we have the "limitation" that we should not store unserializable data, so every single class instance become a plain object. So, I think that doesn't make anymore sense to use Classes.

For example, if my application is a car fleet management app, probably I'lll have some entities like Car, User, etc

Now my question is, where would you manage and instance creation? In the classic OOP I simply do new Car();, but in this case, maybe, the "right way" should be to dispatch the action creator the will do required operation, so dispatch(makeCar()), is this right?

And what if I've some calculated property, like get age?

I'm pretty new to this world...

limdev
  • 706
  • 1
  • 5
  • 18
  • Imagine the redux store to be a database. It should only contain the plain data. If you want to create domain objects from that you would need a tool that can instanciate them given the data from your store or write them to the store as plain data given the domain object. – trixn Sep 21 '20 at 10:15
  • Or in other words you would need an ORM, for example there is [`redux-orm`](https://redux-orm.github.io/redux-orm/) but it might not fit your use case or existing code. – trixn Sep 21 '20 at 10:24
  • @trixn so it would be correct to have classes and simply: 1-create new instance with the class constructor; 2-serialize any object inside the action that store it in the redux store; 3-execute again the constructor when we retrieve an instance from the redux store; ??? – limdev Sep 21 '20 at 10:28
  • Yes roughly speaking that would be needed. But there are other operations like updating an already instanciated domain object. Creating a wrapper around redux that manages creates, updates and deletes of domain objects is a non-trivial task. You can have a look at `redux-orm` to see how they handle it. But it is a fair amount of overhead that comes with that kind of OOP style. Redux is likely not the best fit for that. – trixn Sep 21 '20 at 10:39
  • Also see this answer on a similar question: https://stackoverflow.com/a/43721911/5005177 – trixn Sep 21 '20 at 10:42

0 Answers0