Skip to content

Commit f8b06df

Browse files
vbfoxalfonsogarciacaro
authored andcommitted
Add React PureComponent and Fragment
* Add PureComponent doing shallow state & props comparison to avoid `render()` being called. * Also add a PureStatelessComponent alias to PureComponent taking no state type parameter. Such components are mapping perfectly to an elm-like architecture with immutable props and no mutable state. * Add `Fragment` (class) and `fragment` helper function to easily return lists from `render()`
1 parent 63c263d commit f8b06df

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed

src/Fable.React/Fable.Helpers.React.fs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ module Props =
1414
type IHTMLProp =
1515
inherit IProp
1616

17+
type IFragmentProp =
18+
inherit IProp
19+
20+
type FragmentProp =
21+
| Key of string
22+
interface IFragmentProp
23+
1724
type Prop =
1825
| Key of string
1926
| Ref of (Browser.Element->unit)
@@ -662,6 +669,10 @@ let inline voidEl (tag: string) (props: IHTMLProp list) : ReactElement =
662669
let inline svgEl (tag: string) (props: IProp list) (children: ReactElement list): ReactElement =
663670
createElement(tag, keyValueList CaseRules.LowerFirst props, children)
664671

672+
/// Instantiate a React fragment
673+
let inline fragment (props: IFragmentProp list) (children: ReactElement list): ReactElement =
674+
createElement(typedefof<Fragment>, keyValueList CaseRules.LowerFirst props, children)
675+
665676
// Standard elements
666677
let inline a b c = domEl "a" b c
667678
let inline abbr b c = domEl "abbr" b c

src/Fable.React/Fable.Import.React.fs

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,12 @@ module React =
6565
/// Create a React component by inheriting this class as follows
6666
///
6767
/// ```
68-
/// type MyComponent(props) =
69-
/// inherit React.Component<MyProps, MyState>(props)
68+
/// type MyComponent(initialProps) =
69+
/// inherit React.Component<MyProps, MyState>(initialProps)
7070
/// base.setInitState({ value = 5 })
7171
///
7272
/// override this.render() =
73-
/// // Don't use captured `props` from constructor,
73+
/// // Don't use captured `initialProps` from constructor,
7474
/// // use `this.props` instead (updated version)
7575
/// let msg = sprintf "Hello %s, you have %i €"
7676
/// this.props.name this.state.value
@@ -170,6 +170,40 @@ module React =
170170

171171
interface ReactElement
172172

173+
/// A react component that implements `shouldComponentUpdate()` with a shallow prop and state comparison.
174+
///
175+
/// Usage:
176+
/// ```
177+
/// type MyComponent(initialProps) =
178+
/// inherit React.PureComponent<MyProps, MyState>(initialProps)
179+
/// base.setInitState({ value = 5 })
180+
/// override this.render() =
181+
/// let msg = sprintf "Hello %s, you have %i €"
182+
/// this.props.name this.state.value
183+
/// div [] [ofString msg]
184+
/// ```
185+
and [<AbstractClass; Import("PureComponent", "react")>] PureComponent<[<Pojo>]'P, [<Pojo>]'S>(props: 'P) =
186+
inherit Component<'P, 'S>(props)
187+
188+
/// A react component that implements `shouldComponentUpdate()` with a shallow prop comparison.
189+
///
190+
/// Usage:
191+
/// ```
192+
/// type MyComponent(initialProps) =
193+
/// inherit React.PureStatelessComponent<MyProps>(initialProps)
194+
/// override this.render() =
195+
/// let msg = sprintf "Hello %s, you have %i €"
196+
/// this.props.name this.props.value
197+
/// div [] [ofString msg]
198+
/// ```
199+
and [<AbstractClass; Import("PureComponent", "react")>] PureStatelessComponent<[<Pojo>]'P>(props: 'P) =
200+
inherit Component<'P, obj>(props)
201+
202+
and FragmentProps = { key: string }
203+
204+
and [<Import("Fragment", "react")>] Fragment(props: FragmentProps) =
205+
interface ReactElement
206+
173207
and ClassicComponent<'P, 'S> =
174208
abstract props: 'P with get, set
175209
abstract state: 'S with get, set

0 commit comments

Comments
 (0)