gpt4 book ai didi

reactjs - 如何在 Fable 中实例化自定义 React 组件?

转载 作者:行者123 更新时间:2023-12-04 10:31:19 26 4
gpt4 key购买 nike

我正在尝试使用 Fable 编写一个简单的 React 组件。它应该显示一个带有 + 的简单计数器和 -纽扣。也需要留言string通过 Prop 。
出乎意料的是代码可以编译,但会引发运行时错误:

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.


这是代码:
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 ()
我的 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)

注意:我想使用 setState (而不是钩子(Hook)或 Elmish)出于各种原因。

最佳答案

在您的 render功能,需要使用ofType像这样:

let render () =
ReactDom.render(
ofType<App,_,_> "Counter" [],
document.getElementById "root")
这会渲染一些东西,但是当你点击按钮时什么也没有发生。
要解决此问题,请从 int 更改您的状态到 object :
type state = { count: int }
//..
button [ Props.OnClick (fun _ -> this.setState (fun s _ -> { s with count = s.count + 1 })) ] [ str "+" ]
最后,将您的 Prop 更改为 object .
总而言之,以下应该有效:
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 ()

您可以包装 ofType调用一个方便的函数:
let inline app props children = 
ofType<App,_,_> props children

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

render ()

关于reactjs - 如何在 Fable 中实例化自定义 React 组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60416510/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com