dsuryd / dotNetify

Simple, lightweight, yet powerful way to build real-time web apps.
https://dotnetify.net
Other
1.17k stars 164 forks source link

Parse JSON before State Update in React #231

Closed verystack closed 4 years ago

verystack commented 4 years ago

Hi Dicky,

Does Dotnetify build in any method for Parsing a JSON message sent from the VM before it goes into the React state?

dsuryd commented 4 years ago

No, just using standard JSON.parse.

verystack commented 4 years ago

This is probably a stupid question, but how do I enable parsing of it before it hits the state? Right now I’m stringifying it before sending it to Changed() before PushUpdates(), which of course means I have to Parse it after it hits the state. I’m trying to send a nested string list.

dsuryd commented 4 years ago

You probably need 2 states, one to receive the raw string from the VM, and one connected to the UI that you set when the first state is updated. Use React lifecycle method componentDidUpdate to do this.

With that said, I generally recommend passing data that your component can readily consume.