You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The Type that is output from the toJson method obviously strips any useful information about the original object. This isn't a big deal and is likely expected, but wondered if it was really necessary?
Context
I have a react + redux-toolkit project that is currently using grpc-web via thunks and decided to migrate over to protobuf-es. I ran into an issue storing the protobuf-es objects, as they are instances (with complex field types) and require serialization before storing in Redux.
Thankfully you guys have a toJson method, but it strips all type-safety from the output. I guess this is somewhat expected, and I suspect the expectation is to use ObjectDto.fromJson() on the way out of the redux selectors to restore the instances, but it did create some confusion while working with the serialized object in the reducers (as they no longer have their types). This also means that the Type definition of the store slice has to be be made somewhat loose too.
One option is to first return a serialized output from the thunk (losing any type inference on the action), then deserialize in the reducer, perform any mutations and then reserialize again before storing as state. And then deserialize at the selectors.
While this is doable, losing the type inference of the action and having to deserialize and reserialize is a bit frustrating. This can have a broader impact if you are using dispatch(...).unwrap() and expect to get a typed response directly out of the actions.
An Alternative Approach
In my particular case, the only fields that pose a serializable issue are the Timestamp ones, and I noticed that toJson converts these to DateTime Strings. I decided to create a Type similar to your PlainMessage one to help out (and infers to the converted types of Date/Timestamp, BigInt and Uint8Array):
While I'm not 100% certain this is the best approach, it does seem to be working well for me, for now. It did make me wonder if it was worth adding this kind of Type inference to built into the outputs from the native toJson method? Or if there is a good reason not to? Or maybe if there is a better option to my Redux issues that I just missed?
The text was updated successfully, but these errors were encountered:
The root cause here is that redux expects plain objects, while we use classes. The upcoming v2 should solve this problem - it uses plain objects for proto3 messages, which satisfies redux's constraints.
Similar limitations exist in React Server Components and other frameworks. We expect this new approach to work out of the box in most cases.
The new JSON types feature is another useful addition to the toolset. If you use proto2, messages use the prototype chain to track field presence, which makes them non-plain objects. Going with JSON is a reliable solution in this situation.
Closing this, since the solution will be available as a stable v2 soon. If you give the v2 beta a try, please let us know how it works for you 🙂
Problem
The Type that is output from the
toJson
method obviously strips any useful information about the original object. This isn't a big deal and is likely expected, but wondered if it was really necessary?Context
I have a
react
+redux-toolkit
project that is currently usinggrpc-web
viathunks
and decided to migrate over toprotobuf-es
. I ran into an issue storing the protobuf-es objects, as they are instances (with complex field types) and require serialization before storing in Redux.Thankfully you guys have a
toJson
method, but it strips all type-safety from the output. I guess this is somewhat expected, and I suspect the expectation is to useObjectDto.fromJson()
on the way out of the redux selectors to restore the instances, but it did create some confusion while working with the serialized object in the reducers (as they no longer have their types). This also means that the Type definition of the store slice has to be be made somewhat loose too.One option is to first return a serialized output from the
thunk
(losing any type inference on the action), then deserialize in the reducer, perform any mutations and then reserialize again before storing as state. And then deserialize at the selectors.While this is doable, losing the type inference of the action and having to deserialize and reserialize is a bit frustrating. This can have a broader impact if you are using
dispatch(...).unwrap()
and expect to get a typed response directly out of the actions.An Alternative Approach
In my particular case, the only fields that pose a serializable issue are the
Timestamp
ones, and I noticed thattoJson
converts these to DateTime Strings. I decided to create a Type similar to yourPlainMessage
one to help out (and infers to the converted types of Date/Timestamp, BigInt and Uint8Array):In my case, I am using it in a preconfigured helper function like this:
The Redux slice looks a little like this:
Suggestion
While I'm not 100% certain this is the best approach, it does seem to be working well for me, for now. It did make me wonder if it was worth adding this kind of Type inference to built into the outputs from the native
toJson
method? Or if there is a good reason not to? Or maybe if there is a better option to my Redux issues that I just missed?The text was updated successfully, but these errors were encountered: