11module React.Basic
22 ( Component
3- , StatelessComponent
43 , ComponentSpec
54 , ComponentType
65 , JSX
@@ -14,7 +13,6 @@ module React.Basic
1413 , makeStateless
1514 , asyncEffects
1615 , createComponent
17- , createStatelessComponent
1816 , empty
1917 , keyed
2018 , fragment
@@ -45,9 +43,9 @@ instance monoidJSX :: Monoid JSX where
4543
4644data ComponentType props state action
4745
48- type ComponentSpec props state action =
46+ type ComponentSpec props state initialState action =
4947 { " $$type" :: ComponentType props state action
50- , initialState :: state
48+ , initialState :: initialState
5149 , shouldUpdate :: LimitedSelf props state -> props -> state -> Boolean
5250 , didMount :: Self props state action -> Effect Unit
5351 , didUpdate :: Self props state action -> Effect Unit
@@ -56,9 +54,7 @@ type ComponentSpec props state action =
5654 , render :: Self props state action -> JSX
5755 }
5856
59- type Component = forall props state action . ComponentSpec props state action
60-
61- type StatelessComponent = forall props . ComponentSpec props Void Void
57+ type Component = forall props state action . ComponentSpec props state Void action
6258
6359type Update props state action
6460 = Self props state action
@@ -71,6 +67,25 @@ data StateUpdate props state action
7167 | SideEffects (Self props state action -> Effect Unit )
7268 | UpdateAndSideEffects state (Self props state action -> Effect Unit )
7369
70+ type Self props state action =
71+ { props :: props
72+ , state :: state
73+ , readProps :: Effect props
74+ , readState :: Effect state
75+ , send :: action -> Effect Unit
76+
77+ -- | Unsafe, but still frequently better than rewriting a
78+ -- | whold component in JS
79+ , instance_ :: ReactComponentInstance
80+ }
81+
82+ type LimitedSelf props state =
83+ { props :: props
84+ , state :: state
85+ }
86+
87+ data ReactComponentInstance
88+
7489-- | Convenience function for sending an action asynchronously.
7590-- |
7691-- | Note: potential failure should be handled and converted to an
@@ -146,7 +161,7 @@ buildStateUpdate = case _ of
146161-- | ```
147162foreign import make
148163 :: forall props state action
149- . ComponentSpec props state action
164+ . ComponentSpec props state state action
150165 -> props
151166 -> JSX
152167
@@ -160,78 +175,31 @@ foreign import make
160175-- | ```
161176makeStateless
162177 :: forall props
163- . ComponentSpec props Void Void
178+ . ComponentSpec props Void Void Void
164179 -> (props -> JSX )
165180 -> props
166181 -> JSX
167182makeStateless component render =
168183 make component { render = \self -> render self.props }
169184
170- type Self props state action =
171- { props :: props
172- , state :: state
173- , readProps :: Effect props
174- , readState :: Effect state
175- , send :: action -> Effect Unit
176-
177- -- | Unsafe, but still frequently better than rewriting a
178- -- | whold component in JS
179- , instance_ :: ReactComponentInstance
180- }
181-
182- type LimitedSelf props state =
183- { props :: props
184- , state :: state
185- }
186-
187185data ReactComponent props
188186
189- data ReactComponentInstance
190-
191187createComponent
192- :: String
193- -> { " $$type" :: forall props state action . ComponentType props state action
194- , initialState :: forall state . state
195- , shouldUpdate :: forall props state . LimitedSelf props state -> props -> state -> Boolean
196- , didMount :: forall props state action . Self props state action -> Effect Unit
197- , didUpdate :: forall props state action . Self props state action -> Effect Unit
198- , willUnmount :: forall props state action . LimitedSelf props state -> Effect Unit
199- , update :: forall props state action . Update props state action
200- , render :: forall props state action . Self props state action -> JSX
201- }
188+ :: forall props state action
189+ . String
190+ -> ComponentSpec props state Void action
202191createComponent = runFn3 createComponent_ NoUpdate buildStateUpdate
203192
204193foreign import createComponent_
205- :: Fn3
206- (forall props state action . StateUpdate props state action )
207- (forall props state action . StateUpdate props state action
194+ :: forall props state action
195+ . Fn3
196+ (StateUpdate props state action )
197+ (StateUpdate props state action
208198 -> { state :: Nullable state
209199 , effects :: Nullable (Self props state action -> Effect Unit )
210200 } )
211201 String
212- ({ " $$type" :: forall props state action . ComponentType props state action
213- , initialState :: forall state . state
214- , shouldUpdate :: forall props state . LimitedSelf props state -> props -> state -> Boolean
215- , didMount :: forall props state action . Self props state action -> Effect Unit
216- , didUpdate :: forall props state action . Self props state action -> Effect Unit
217- , willUnmount :: forall props state action . LimitedSelf props state -> Effect Unit
218- , update :: forall props state action . Update props state action
219- , render :: forall props state action . Self props state action -> JSX
220- } )
221-
222- -- | Creates a named, stateless component
223- createStatelessComponent
224- :: String
225- -> { " $$type" :: forall props state action . ComponentType props Void Void
226- , initialState :: Void
227- , shouldUpdate :: forall props . LimitedSelf props Void -> props -> Void -> Boolean
228- , didMount :: forall props . Self props Void Void -> Effect Unit
229- , didUpdate :: forall props . Self props Void Void -> Effect Unit
230- , willUnmount :: forall props . LimitedSelf props Void -> Effect Unit
231- , update :: forall props . Update props Void Void
232- , render :: forall props . Self props Void Void -> JSX
233- }
234- createStatelessComponent = createComponent
202+ (ComponentSpec props state Void action )
235203
236204-- | An empty node. This is often useful when you would like to conditionally
237205-- | show something, but you don't want to (or can't) modify the `children` prop
@@ -267,14 +235,28 @@ element = runFn2 element_
267235
268236-- | Like `element`, plus a `key` for rendering components in a dynamic list.
269237-- | For more information see: https://reactjs.org/docs/reconciliation.html#keys
270- elementKeyed :: forall props . ReactComponent { | props } -> { key :: String | props } -> JSX
238+ elementKeyed
239+ :: forall props
240+ . ReactComponent { | props }
241+ -> { key :: String | props }
242+ -> JSX
271243elementKeyed = runFn2 elementKeyed_
272244
273- foreign import element_ :: forall props . Fn2 (ReactComponent { | props } ) { | props } JSX
245+ foreign import element_
246+ :: forall props
247+ . Fn2 (ReactComponent { | props } ) { | props } JSX
274248
275- foreign import elementKeyed_ :: forall props . Fn2 (ReactComponent { | props } ) { key :: String | props } JSX
249+ foreign import elementKeyed_
250+ :: forall props
251+ . Fn2 (ReactComponent { | props } ) { key :: String | props } JSX
276252
277- foreign import toReactComponent :: forall props state action . ComponentSpec { | props } state action -> ReactComponent { | props }
253+ foreign import toReactComponent
254+ :: forall props state action
255+ . ComponentSpec { | props } state state action
256+ -> ReactComponent { | props }
278257
279- displayName :: forall props state action . ComponentSpec props state action -> String
258+ displayName
259+ :: forall props state initialState action
260+ . ComponentSpec props state initialState action
261+ -> String
280262displayName = _.displayName <<< unsafeCoerce <<< _." $$type"
0 commit comments