'Is there a way to use selector with createApi to handle custom mapping
I am learning redux toolkit with react, I want to do a custom mapping from the response of API to my interface. I created a slice using createApi function, but I don't know how to do the mapping using selectors. Example: the response of API return a json object {"first_name": "Toto", "last_name": "Foo"} and my interface is defined with this way:
interface user { firstname: string, lastname: string }
In the old way we create a selector with the state as an input and I did a classic mapping field to field.
Solution 1:[1]
You can do that in transformResponse for the endpoint - then it will already be stored in the cache in the right format.
See Customizing query responses with transformResponse
In your case:
transformResponse(
baseQueryReturnValue: unknown,
) {
return {
firstname: (baseQueryReturnValue as any).first_name,
lastname: (baseQueryReturnValue as any).last_name,
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | phry |
