Ziac
Ziac

Reputation: 70

Using the word "type" in redux actions

I'm trying to use Redux with React Native recently. As far as I know, the word "type" is restricted by the Redux, that is, if there is a property in my property, it won't work together.

{ 
  type: 'MODIFY_USER',
  gender: 'male',
  type: 'normal_user', // <= this is the type of the user specified to modify,
}

Since every naming is perfectly mapped in my code. How should I adapt with this??

Upvotes: 0

Views: 93

Answers (2)

Pedro Brost
Pedro Brost

Reputation: 1452

You can take the "payload" approach:

{ 
  type: 'MODIFY_USER',
  payload: {
    gender: 'male',
    type: 'normal_user', // <= this is the type of the user specified to modify,
  }
}

Upvotes: 3

JRK
JRK

Reputation: 3904

Can you build an adapter which takes your modelled data and adapts it to the API?

For example, say you have your original code base. Once you have modelled your data, then pass it through an Adapter.

You can map the new type, say user_type to the original.

For example:

Incoming: Component > Adapter > Redux

Outgoing: Redux > Adapter > Component

With an adapter you don't have to change your code to suit other APIs. For example if you want to pass your user data to Facebook, you can create a Facebook Adapter in your application.

I think the good thing about adapters is that you never change your code to suit various APIs, you only create the adapters (which creates an easier flow, rather than manipulating your data within your current application)

Upvotes: 1

Related Questions