Skip to content

Commit e9f77d7

Browse files
committed
Change ReactContext to be foreign data type
1 parent eb19b8f commit e9f77d7

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

src/React/Basic.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,15 @@ exports.toReactComponent = function(_unionDict) {
220220
};
221221

222222
exports.createContext = function(defaultValue) {
223-
var context = React.createContext(defaultValue);
224-
return {
225-
consumer: context.Consumer,
226-
provider: context.Provider
223+
return function () {
224+
return React.createContext(defaultValue);
227225
};
228226
};
227+
228+
exports.contextProvider = function (context) {
229+
return context.Provider;
230+
};
231+
232+
exports.contextConsumer = function (context) {
233+
return context.Consumer;
234+
};

src/React/Basic.purs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ module React.Basic
2323
, Ref
2424
, ReactContext
2525
, createContext
26+
, contextConsumer
27+
, contextProvider
2628
, provider
2729
, consumer
2830
) where
@@ -373,14 +375,11 @@ foreign import toReactComponent
373375
-> { render :: Self props state -> JSX | spec }
374376
-> ReactComponent { | jsProps }
375377

376-
type ReactContext a =
377-
{ provider :: ReactComponent { value :: a, children :: Array JSX }
378-
, consumer :: ReactComponent { children :: a -> Array JSX }
379-
}
378+
foreign import data ReactContext :: Type -> Type
380379

381380
-- | Create a `ReactContext` given a default value. Use `provider` and `consumer`
382-
-- | to provide and consume context values. Alternatively, use the fields of
383-
-- | `ReactContext` directly if a `ReactComponent` is required for interop.
381+
-- | to provide and consume context values. Alternatively, use `contextProvider`
382+
-- | and `contextConsumer` directly if a `ReactComponent` is required for interop.
384383
-- |
385384
-- | ```purs
386385
-- | render self =
@@ -398,21 +397,31 @@ type ReactContext a =
398397
-- | ```
399398
-- |
400399
-- | __*See also:* `provider`, `consumer`, React's documentation regarding Context__
401-
foreign import createContext :: forall a. a -> ReactContext a
400+
foreign import createContext :: forall a. a -> Effect (ReactContext a)
401+
402+
foreign import contextProvider
403+
:: forall a
404+
. ReactContext a
405+
-> ReactComponent { value :: a, children :: Array JSX }
406+
407+
foreign import contextConsumer
408+
:: forall a
409+
. ReactContext a
410+
-> ReactComponent { children :: a -> Array JSX }
402411

403412
-- | Create a provider `JSX` given a context value and children.
404413
-- |
405414
-- | __*See also:* `createContext`, `consumer`__
406415
provider :: forall a. ReactContext a -> a -> Array JSX -> JSX
407416
provider context value children =
408-
element context.provider { value, children }
417+
element (contextProvider context) { value, children }
409418

410419
-- | Create a consumer `JSX` from a context value to children.
411420
-- |
412421
-- | __*See also:* `createContext`, `producer`__
413422
consumer :: forall a. ReactContext a -> (a -> Array JSX) -> JSX
414423
consumer context children =
415-
element context.consumer { children }
424+
element (contextConsumer context) { children }
416425

417426
-- |
418427
-- | Internal utility or FFI functions

0 commit comments

Comments
 (0)