-
Notifications
You must be signed in to change notification settings - Fork 40
Add support for React Context #101
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,10 @@ module React.Basic | |
, ReactComponentInstance | ||
, toReactComponent | ||
, Ref | ||
, ReactContext | ||
, createContext | ||
, provider | ||
, consumer | ||
) where | ||
|
||
import Prelude | ||
|
@@ -369,6 +373,46 @@ foreign import toReactComponent | |
-> { render :: Self props state -> JSX | spec } | ||
-> ReactComponent { | jsProps } | ||
|
||
type ReactContext a = | ||
{ provider :: ReactComponent { value :: a, children :: Array JSX } | ||
, consumer :: ReactComponent { children :: a -> Array JSX } | ||
} | ||
|
||
-- | Create a `ReactContext` given a default value. Use `provider` and `consumer` | ||
-- | to provide and consume context values. Alternatively, use the fields of | ||
-- | `ReactContext` directly if a `ReactComponent` is required for interop. | ||
-- | | ||
-- | ```purs | ||
-- | render self = | ||
-- | R.div_ | ||
-- | [ R.button | ||
-- | { onClick: capture_ $ self.setState \s -> s { counter = s.counter + 1 } | ||
-- | , children: [ R.text "Tick!" ] | ||
-- | } | ||
-- | , provider countContext self.state.counter | ||
-- | [ consumer countContext \counter -> | ||
-- | [ R.text $ "Ticks: " <> (show counter) | ||
-- | ] | ||
-- | ] | ||
-- | ] | ||
-- | ``` | ||
-- | | ||
-- | __*See also:* `provider`, `consumer`, React's documentation regarding Context__ | ||
foreign import createContext :: forall a. a -> ReactContext a | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should have the type |
||
|
||
-- | Create a provider `JSX` given a context value and children. | ||
-- | | ||
-- | __*See also:* `createContext`, `consumer`__ | ||
provider :: forall a. ReactContext a -> a -> Array JSX -> JSX | ||
provider context value children = | ||
element context.provider { value, children } | ||
|
||
-- | Create a consumer `JSX` from a context value to children. | ||
-- | | ||
-- | __*See also:* `createContext`, `producer`__ | ||
consumer :: forall a. ReactContext a -> (a -> Array JSX) -> JSX | ||
maddie927 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
consumer context children = | ||
element context.consumer { children } | ||
|
||
-- | | ||
-- | Internal utility or FFI functions | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👋 I'm late to the party as this PR has been merged for quite some time, and maybe my following question should have been posted elsewhere. if that's wrong I do apologize 🙇
I've tried using the context API but failed to create a context as it's actually an Effect. This example doesn't cover the creation part. Am I missing something?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello 👋
You have two options:
unsafePerformEffect
at the module level to create your context. It bends the rules a tiny bit, but in a safe, predictable way.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry I missed the notification of your reply. Thank you very much, it confirms what I was suspecting.
I'll go with the first option as I don't have a purescript main entry point in my setup.
The second option is the most appealing obviously. My take on all of this would be that if you have an main entry-point you could totally avoid React context and simply use a MonadReader-like instead (assuming we don't care about interop I guess).
Thank you @spicydonuts 🙇