Skip to content

Add React.memo #112

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 6, 2018
Merged

Add React.memo #112

merged 4 commits into from
Nov 6, 2018

Conversation

vbfox
Copy link
Contributor

@vbfox vbfox commented Nov 5, 2018

It also add to the helper types the concept of a ReactComponentType (Something that can be passed to createElement). Some of the types exists in the typescript-imported part of the library but they aren't really adapted and try to handle lot of things that are now obsolete.

They will also be necessary to import React.lazy

I didn't add any higher level helpers but one I find useful is the capability to create a named memo without passing the comparer :

let namedMemo<'props> (name: string) (f: 'props -> ReactElement): ReactComponentType<'props> =
    f?displayName <- name
    memo(f, None)

// Usage
type HelloProps = { Name: string }
let helloWorldComponent = namedMemo "HelloWorld" (fun props ->
  span [] [str "Hello "; str props.Name]
)
let helloWorld name = ofElementType helloWorldComponent { Name = name } []

Should I add it too ?

PS: No server-side rendering for now as I don't know how it should work.

Fixes #111

No server-side rendering for now

Fixes #111
@MangelMaxime
Copy link
Member

I think you should add the namedMemo function.

@alfonsogarciacaro
Copy link
Member

Thanks @vbfox! Looks good 👍 I just left a couple of comments. It would be nice to have namedMemo too, although I'm not sure how to mix that with my proposal of having two functions memo and memoWith 🤔

@alfonsogarciacaro
Copy link
Member

Another option could be to use a static type helper so we can use optional arguments (areEqual, name)... But I know @MangelMaxime doesn't like optional arguments XD

@alfonsogarciacaro
Copy link
Member

Or a list of attributes in line with other React components:

memo [Name "foo"; AreEqual (fun p1 p2 -> p2.x = p2.x)] (fun props ->
   ...)

@MangelMaxime
Copy link
Member

The list of attributes style is a bit more verbose, but like that we can have only one entry point for the function. I kind of like it :)

@vbfox
Copy link
Contributor Author

vbfox commented Nov 5, 2018

After reflection I don't think that versions without name are useful for Fable developers, might as well guide people toward having a nicely named element tree.

I'll provide memo and memoWith with names.

@vbfox
Copy link
Contributor Author

vbfox commented Nov 5, 2018

type HelloProps = { Name: string }

let helloWorldComponent = memo "HelloWorld" (fun props ->
  span [] [str "Hello "; str props.Name]
)
let helloWorld name = ofElementType helloWorldComponent { Name = name } []

let helloWorldComponent2 = memoWith "HelloWorld" (fun a b -> a.Name = b.Name) (fun props ->
  span [] [str "Hello "; str props.Name]
)
let helloWorld2 name = ofElementType helloWorldComponent2 { Name = name } []

@alfonsogarciacaro
Copy link
Member

Awesome, thanks a lot!

@alfonsogarciacaro
Copy link
Member

I've published this as Fable.React 5.0.0-alpha-001 (at the end I used memoBuilder for the name, sorry!). I've also tried to adapt it to SSR, @forki can you please give it a try? Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants