Skip to content

Commit b5ebb89

Browse files
author
Michael Trotter
committed
Make it more difficult to run into the incompatible props bug
1 parent 0fb25d1 commit b5ebb89

7 files changed

Lines changed: 25 additions & 18 deletions

File tree

examples/component/src/Container.purs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import React.Basic (Component, JSX, createComponent, makeStateless)
66
import React.Basic.DOM as R
77
import ToggleButton (toggleButton)
88

9+
component :: Component Unit
10+
component = createComponent "Container"
11+
912
toggleButtonContainer :: JSX
1013
toggleButtonContainer = unit # makeStateless component \_ ->
1114
R.div
@@ -14,6 +17,3 @@ toggleButtonContainer = unit # makeStateless component \_ ->
1417
, toggleButton { label: "B" }
1518
]
1619
}
17-
18-
component :: Component
19-
component = createComponent "Container"

examples/component/src/ToggleButton.purs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import Effect.Console (log)
66
import React.Basic (Component, JSX, StateUpdate(..), capture_, createComponent, make)
77
import React.Basic.DOM as R
88

9+
component :: Component Props
10+
component = createComponent "ToggleButton"
11+
912
type Props =
1013
{ label :: String
1114
}
@@ -37,6 +40,3 @@ toggleButton = make component
3740
]
3841
}
3942
}
40-
41-
component :: Component
42-
component = createComponent "ToggleButton"

examples/controlled-input/src/ControlledInput.purs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ import React.Basic.DOM as R
99
import React.Basic.DOM.Events (targetValue, timeStamp)
1010
import React.Basic.Events (merge)
1111

12-
type Props = {}
12+
component :: Component Props
13+
component = createComponent "ControlledInput"
14+
15+
type Props = Unit
1316

1417
data Action
1518
= ValueChanged String Number
@@ -40,6 +43,3 @@ controlledInput = make component
4043
, R.p_ [ R.text ("Changed at = " <> maybe "never" show self.state.timestamp) ]
4144
]
4245
}
43-
44-
component :: Component
45-
component = createComponent "ControlledInput"

examples/controlled-input/src/Main.purs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ main = do
1818
case container of
1919
Nothing -> throw "Container element not found."
2020
Just c ->
21-
let app = controlledInput {}
21+
let app = controlledInput unit
2222
in render app c

examples/counter/src/Counter.purs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import Prelude
55
import React.Basic (Component, JSX, StateUpdate(..), capture_, createComponent, make)
66
import React.Basic.DOM as R
77

8-
component :: Component
8+
component :: Component Props
99
component = createComponent "Counter"
1010

1111
type Props =

examples/legacy-v2/src/Compat.purs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module React.Basic.Compat
88
import Prelude
99

1010
import Effect (Effect)
11-
import React.Basic (ComponentSpec, JSX, ReactComponent, Self, StateUpdate(..), createComponent, element, elementKeyed, empty, fragment, fragmentKeyed, make, makeStateless, send, toReactComponent)
11+
import React.Basic (JSX, ReactComponent, Self, StateUpdate(..), createComponent, element, elementKeyed, empty, fragment, make, makeStateless, send, toReactComponent)
1212

1313
type Component = ReactComponent
1414

src/React/Basic.purs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ import React.Basic.Events (EventFn, EventHandler, SyntheticEvent, handler)
7373
-- | For example:
7474
-- |
7575
-- | ```purs
76-
-- | component :: Component
76+
-- | component :: Component Props
7777
-- | component = createComponent "Counter"
7878
-- |
7979
-- | type Props =
@@ -124,7 +124,7 @@ type ComponentSpec props state initialState action =
124124
-- | The resulting component spec is usually given the simplified `Component` type:
125125
-- |
126126
-- | ```purs
127-
-- | component :: Component
127+
-- | component :: Component Props
128128
-- | component = createComponent "Counter"
129129
-- | ```
130130
-- |
@@ -133,6 +133,13 @@ type ComponentSpec props state initialState action =
133133
-- | `JSX` tree is a valid update, or if it needs to be replaced entirely
134134
-- | (expensive and clears component state lower in the tree).
135135
-- |
136+
-- | __*Note:* A specific type for the props in `Component props` should always be chosen at this point.
137+
-- | It's technically possible to declare the component with `forall props. Component props`
138+
-- | but doing so is unsafe. Leaving the prop type open allows the use of a single `Component`
139+
-- | definition in multiple React-Basic components that may have different prop types. Because
140+
-- | component lifecycles are managed by React, it's possible for incompatible prop values to
141+
-- | be passed into a lifecycle function.__
142+
-- |
136143
-- | __*Note:* A `Component` is *not* a valid React component by itself. If you would like to use
137144
-- | a React-Basic component from JavaScript, use `toReactComponent`.__
138145
-- |
@@ -150,7 +157,7 @@ createComponent =
150157

151158
-- | A simplified alias for `ComponentSpec`. This type is usually used to represent
152159
-- | the default component type returned from `createComponent`.
153-
type Component = forall props state action. ComponentSpec props state Unit action
160+
type Component props = forall state action. ComponentSpec props state Unit action
154161

155162
-- | Opaque component information for internal use.
156163
-- |
@@ -258,7 +265,7 @@ foreign import readState :: forall props state action. Self props state action -
258265
-- | This is where you will want to provide customized implementations:
259266
-- |
260267
-- | ```purs
261-
-- | component :: Component
268+
-- | component :: Component Props
262269
-- | component = createComponent "Counter"
263270
-- |
264271
-- | type Props =
@@ -294,7 +301,7 @@ foreign import make
294301
-- | Makes stateless component definition slightly less verbose:
295302
-- |
296303
-- | ```purs
297-
-- | component :: Component
304+
-- | component :: Component Props
298305
-- | component = createComponent "Xyz"
299306
-- |
300307
-- | myComponent :: Props -> JSX

0 commit comments

Comments
 (0)