use-profunctor-state icon indicating copy to clipboard operation
use-profunctor-state copied to clipboard

Does promap have to return another hook?

Open skokenes opened this issue 6 years ago • 2 comments

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?

skokenes avatar Jan 25 '19 23:01 skokenes

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.

staltz avatar Jan 26 '19 10:01 staltz

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 ?

halcaponey avatar Oct 27 '19 20:10 halcaponey