staltz / use-profunctor-state

React Hook for state management with profunctor lenses
MIT License
333 stars 11 forks source link

Does promap have to return another hook? #5

Open skokenes opened 5 years ago

skokenes commented 5 years ago

Looking for some feedback here on a potential feature I might add to a fork, but I'm not sure if it makes sense since I'm new to lenses.

In this implementation of useProfunctorState, promap always returns a new ProfunctorState which is reliant on a hook. This is nice for defining lenses in children components.

But I am having trouble using it to dynamically create lenses in a single component. There are scenarios where I might want to use lenses "on the fly" inside a component, without creating a new hook but rather relying on the underlying hook of the parent profunctor. Like perhaps I want to create a profunctor for every item in a list of variable size. This won't work because you can't create hooks this way, but I don't really want to create a hook. I want to leverage the existing hook, but lens it without creating a new hook.

My questions are: a) is this even a good idea to do, or does another architecture make more sense? b) is it possible today given the existing API in some way that I'm not seeing?

staltz commented 5 years ago

I understand the problem you're describing, I also faced it once, but here's how I found how to solve it: https://codesandbox.io/s/mn73v8yr8

So it seems to be a matter of documentation: how to explain how to handle dynamic lists.

halcaponey commented 5 years ago

Another solution would be to not useMemo every promap so we could promap inside loops and conditionals.

I've put together a POC where if args is set to false then useMemo is not called.

I don't know how this affects performance.

I'd like your take on this @staltz ?