purescript-react/purescript-react-basic-hooks

How to use useContext, ContextProvider, createContext?

seanyu4296 opened this issue ยท 5 comments

Just wondering do you have any examples where you use useContext, ContextProvider, createContext? I'm pretty new to purescript, and have been using purescript-react-basic for awhile only. I would love to use React's Context API. so i see the type of createContext is
Effect (Context a). Does that mean i need to put it somewhere like on a didMount of my Root component to get Context a? Thank you.

At the moment you most likely will want to call createContext at the module level using unsafePerformEffect. This isn't ideal but I haven't thought of a good way around it yet. This is exactly how it works in JS React apps though, so it seemed like the best combination of tradeoffs until I think of something better.

I'll leave this open until I get an example added at least though :)

Alright that's what i ended up doing actually and it worked fine, still thinking about it if it's worth using in production. Maybe I can PR an example soon, after i migrate to using hooks. I'm currently using the FFI you did for React Context, but with the consumer provider pattern.

Thanks for this library and quick response!

Hey @megamaddu ! Do you have another example for creating a context? It seems that the file you linked has been deleted.

I would really appreciate it ๐Ÿ˜