sdgfsdh
sdgfsdh

Reputation: 37131

How to instantiate a custom React Component in Fable?

I am trying to write a simple React component using Fable. It should take display a simple counter with + and - buttons. It also takes a message string via props.

What is unexpected is that the code compiles, but it throws a run-time error:

Error: Objects are not valid as a React child (found: object with keys {props, context, refs, updater, state}).

If you meant to render a collection of children, use an array instead.

Here is the code:

module App

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

type App (message) =
  inherit Component<string, int> (message) with

    do
      base.setInitState(0)

    override this.render () =
      div
        []
        [
          h1 [] [ str (sprintf "%s - %i" this.props this.state) ]
          button [ Props.OnClick (fun _ -> this.setState (fun s _ -> s + 1)) ] [ str "+" ]
          button [ Props.OnClick (fun _ -> this.setState (fun s _ -> s - 1)) ] [ str "-" ]
        ]


let render () =
  ReactDom.render(
    App "Counter",
    document.getElementById "root")

render ()

My paket.lock:

STORAGE: NONE
RESTRICTION: || (== netcoreapp3.1) (== netstandard2.0) (== netstandard2.1)
NUGET
  remote: https://api.nuget.org/v3/index.json
    Fable.Browser.Blob (1.1)
      Fable.Core (>= 3.0)
      FSharp.Core (>= 4.6.2)
    Fable.Browser.Dom (1.1)
      Fable.Browser.Blob (>= 1.1)
      Fable.Browser.Event (>= 1.0)
      Fable.Browser.WebStorage (>= 1.0)
      Fable.Core (>= 3.0)
      FSharp.Core (>= 4.6.2)
    Fable.Browser.Event (1.0)
      Fable.Core (>= 3.0)
      FSharp.Core (>= 4.5.2)
    Fable.Browser.WebStorage (1.0)
      Fable.Browser.Event (>= 1.0)
      Fable.Core (>= 3.0)
      FSharp.Core (>= 4.5.2)
    Fable.Core (3.1.5)
      FSharp.Core (>= 4.7)
    Fable.React (5.3.6)
      Fable.Browser.Dom (>= 1.0)
      Fable.Core (>= 3.0)
      FSharp.Core (>= 4.7)
    FSharp.Core (4.7)

Note: I want to use setState (rather than hooks or Elmish) for various reasons.

Upvotes: 2

Views: 495

Answers (1)

nilekirk
nilekirk

Reputation: 2393

In your render function, you need to use ofType like so:

let render () =
  ReactDom.render(
    ofType<App,_,_> "Counter" [],
    document.getElementById "root")

This get things to render, but nothing happens when you click on the buttons.

To fix this, change your state from an int to an object:

type state = { count: int }
//..
button [ Props.OnClick (fun _ -> this.setState (fun s _ -> { s with count = s.count + 1 })) ] [ str "+" ]

Finally, also change your props to be an object.

All in all, the following should work:

type props = { message: string }
type state = { count: int }

type App (props) =
  inherit Component<props, state> (props) with

    do
      base.setInitState({ count = 0 })

    override this.render () =
      div
        []
        [
          h1 [] [ str (sprintf "%s - %i" this.props.message this.state.count) ]
          button [ Props.OnClick (fun _ -> this.setState (fun s _ -> { s with count = s.count + 1 })) ] [ str "+" ]
          button [ Props.OnClick (fun _ -> this.setState (fun s _ -> { s with count = s.count - 1 })) ] [ str "-" ]
        ]


let render () =
  ReactDom.render(
    ofType<App,_,_> { message = "Counter" } [],
    document.getElementById "root")

render ()

You can wrap the ofType call in a convenience function:

let inline app props children = 
  ofType<App,_,_> props children

let render () =
  ReactDom.render(
    app { message = "Counter" } [],
    document.getElementById "root")
    
render ()

Upvotes: 2

Related Questions