Skip to content
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

Add forwardRef hook #177

Open
cmeeren opened this issue Aug 24, 2019 · 6 comments
Open

Add forwardRef hook #177

cmeeren opened this issue Aug 24, 2019 · 6 comments

Comments

@cmeeren
Copy link
Contributor

cmeeren commented Aug 24, 2019

Material-UI often requires forwardRef if composing using functional components. I couldn't find the forwardRef hook in Fable.React. Would be great to have it!

@nojaf
Copy link
Member

nojaf commented Aug 25, 2019

Are you sure? Don't you want https://github.com/fable-compiler/fable-react/blob/master/src/Fable.React.Hooks.fs#L43 in your case?

@cmeeren
Copy link
Contributor Author

cmeeren commented Aug 25, 2019

Based on the doc section I linked to, then yes, I'm pretty sure I need forwardRef, not useRef.

@nojaf
Copy link
Member

nojaf commented Aug 25, 2019

My bad, you are correct.

This works for me in the repl

F#:

open Fable.Core.JsInterop
open Fable.React
open Fable.React.Props
open Browser.Types
open Fable.Core

[<Global("React.forwardRef")>]
let forwardRef<'Props,'Ref> (render:('Props -> 'Ref -> ReactElement)) : ReactElementType = jsNative
 // import "forwardRef" "react"

let EmailComponent =
    forwardRef<_, _> (fun _ ref -> 
        input [Ref ref]
    )

let App =
    FunctionComponent.Of(
        fun () -> 
            let emailRef = Hooks.useRef(JS.undefined<HTMLInputElement>)
            let onClick _ =
                emailRef.current.focus()

            div [] [
                ReactBindings.React.createElement(EmailComponent, {| ref = emailRef |}, [])
                button [OnClick onClick] [str "Click me to focus email"]
            ]

        , "App"
    )

mountById "app" (App())

html:

<!doctype html>
<html>
<head>
  <meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <script src="__HOST__/libs/react.production.min.js"></script>
  <script src="__HOST__/libs/react-dom.production.min.js"></script>
</head>
<body>
  <div id="app"></div>
</body>
</html>

So in your code you probably want something like:

let forwardRef<'Props,'Ref> (render:('Props -> 'Ref -> ReactElement)) : ReactElementType = import "forwardRef" "react"

@vbfox any thoughts on this?

@Luiz-Monad
Copy link
Contributor

Luiz-Monad commented Aug 25, 2019

I added it, fa43005 ( I had the same problem with MUI, you need fwRef for HoCs )

I guess the nuget package wasn't released yet, but it's merged.

@cmeeren
Copy link
Contributor Author

cmeeren commented Aug 25, 2019

Great! Any chance to get this deployed? I see it's been quite a while since it was comitted.

@alfonsogarciacaro
Copy link
Member

Ups, seems I merged that right after coming back from holidays and forgot to release a new version, sorry! I'll do it right away 🚀

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

No branches or pull requests

4 participants