Skip to content

Commit 7b6f98c

Browse files
vbfoxalfonsogarciacaro
authored andcommitted
Add React.memo
No server-side rendering for now Fixes #111
1 parent dbead22 commit 7b6f98c

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -851,6 +851,14 @@ let inline fn<'P> (f: 'P -> ReactElement) (props: 'P) (children: ReactElement se
851851
let inline ofImport<'P> (importMember: string) (importPath: string) (props: 'P) (children: ReactElement seq): ReactElement =
852852
createElement(import importMember importPath, props, children)
853853

854+
[<Erase>]
855+
type ReactElementType<'props> = interface end
856+
857+
[<Erase>]
858+
type ReactComponentType<'props> =
859+
inherit ReactElementType<'props>
860+
abstract displayName: string option with get, set
861+
854862
#if FABLE_COMPILER
855863
/// Alias of `ofString`
856864
let inline str (s: string): ReactElement = unbox s
@@ -878,6 +886,40 @@ let inline ofList (els: ReactElement list): ReactElement = unbox(List.toArray el
878886
/// Returns an array **from .render() method**
879887
let inline ofArray (els: ReactElement array): ReactElement = unbox els
880888

889+
[<RequireQualifiedAccess>]
890+
module ReactElementType =
891+
let inline ofComponent<'comp, 'props, 'state when 'comp :> Component<'props, 'state>> : ReactComponentType<'props> =
892+
unbox jsConstructor<'comp>
893+
894+
let inline ofFunction<'props> (f: 'props -> ReactElement) : ReactComponentType<'props> =
895+
unbox f
896+
897+
let inline ofHtmlElement<'props> (name: string): ReactElementType<'props> =
898+
unbox name
899+
900+
/// Create a ReactElement to be rendered from an element type, props and children
901+
let inline create<'props> (comp: ReactElementType<'props>) (props: 'props) (children: ReactElement seq): ReactElement =
902+
createElement(comp, props, children)
903+
904+
type PropsEqualityComparison<'props> = 'props -> 'props -> bool
905+
906+
/// React.memo is a higher order component. It’s similar to React.PureComponent but for function components instead of
907+
/// classes.
908+
///
909+
/// If your function component renders the same result given the same props, you can wrap it in a call to React.memo
910+
/// for a performance boost in some cases by memoizing the result. This means that React will skip rendering the
911+
/// component, and reuse the last rendered result.
912+
///
913+
/// By default it will only shallowly compare complex objects in the props object. If you want control over the
914+
/// comparison, you can also provide a custom comparison function as the second argument.
915+
[<Import("memo", from="react")>]
916+
let memo<'props> (render: 'props -> ReactElement, areEqual: PropsEqualityComparison<'props> option) : ReactComponentType<'props> =
917+
jsNative
918+
919+
/// Create a ReactElement to be rendered from an element type, props and children
920+
let inline ofElementType<'props> (comp: ReactElementType<'props>) (props: 'props) (children: ReactElement seq): ReactElement =
921+
ReactElementType.create comp props children
922+
881923
#else
882924
/// Alias of `ofString`
883925
let inline str (s: string): ReactElement = HTMLNode.Text s :> ReactElement

0 commit comments

Comments
 (0)