Apollo-client: add hook for reactive variable

Created on 10 Aug 2020  ·  4Comments  ·  Source: apollographql/apollo-client

I would like to use simple react hook for rerender my component(instead of query) when reactive variable was changed.

For example useReactiveVariable(myReactiveVariable);

idea ⚛️ React-related 🍪 feature-request 🛬 fixed-in-prerelease 🧞‍♂️ enhancement

Most helpful comment

@seeden You can try this out in @apollo/[email protected] (released yesterday)!

// Either way works:
import { useReactiveVar } from "@apollo/client"
import { useReactiveVar } from "@apollo/client/react/hooks"

All 4 comments

This would definitely be a worthwhile feature.

In terms of the implementation, I believe reactive variables would need to have a more general-purpose listener API. Right now updating a reactive variable calls cache.broadcastWatches() on any caches previously involved in reading the variable, but that behavior could be generalized/exposed via myVar.listen(newValue => {...}) or something similar.

@benjamn I am glad that you like this idea. Thank you

@seeden You can try this out in @apollo/[email protected] (released yesterday)!

// Either way works:
import { useReactiveVar } from "@apollo/client"
import { useReactiveVar } from "@apollo/client/react/hooks"

thanks @benjamn

Was this page helpful?
0 / 5 - 0 ratings