reflex icon indicating copy to clipboard operation
reflex copied to clipboard

Better (or more clearly documented) ways to have component states

Open mwesterhof opened this issue 3 years ago β€’ 3 comments

I've had a great time playing around with pynecone, and it seems very promising.

One thing that trips me up though, i've been trying to create components in a project that are aware of their own states, somewhat similar to how you could simply use the "useState" hook in any isolated react component. So far, I haven't found a way to make this work properly. I find myself passing the app state as a parameter to whatever component function I'm calling, and managing absolutely everything in the central state.

Now, the documentation mentions substates, but there seem to be 2 issues with that;

  1. a lack of clarity about real life applications, and nothing informative in the pynecone examples either
  2. they seem to rely on subclassing the root state, apparently. If i want an isolated component to manage its own state (and define it in a separate module), that doesn't seem very viable. Is there a way for an imported component to manage its own isolated state? If so, it would be worth showing in the documentation, i think

mwesterhof avatar Dec 20 '22 09:12 mwesterhof

Yes, I am looking for a way to divide my application to lots of components, that I can reuse it or even publish it to PyPI, a Pynecone component library, like the React things.

FHU-yezi avatar Dec 20 '22 13:12 FHU-yezi

Thanks for raising this issue. Currently all state has to be managed through the app state (or substates). I agree the documentation is lacking here, we will improve it.

But you're right even with this method, you would have to hook up the state to each instance of the component you create. There's not a simple way for the component to manage it's own isolated state independent from the rest of the app. I think this seems super useful though so we will think about how we can add this.

picklelo avatar Dec 20 '22 17:12 picklelo

I agree that it would be a great improvement. as @FHU-yezi mentioned, being able to publish sophisticated frontend components to PyPI would be a major benefit. I also understand that there's no simple way to manage this given the current way pynecone implements state (that was my impression after all), but I'd suggest that it might be better to mimic react's handling of states a bit more closely. Certainly having something like a useState hook would be a major benefit.

mwesterhof avatar Dec 20 '22 17:12 mwesterhof