我需要使用寓言 react 有状态组件与elmish调度。我不知道如何创建它。

我正在使用此项目模板:https://github.com/fable-elmish/templates

这是模型:

module Home.Types

    [<Pojo>]
    type Model = {
        isLoading:                  bool
    }

这是组件:
type Documentation(props) as this =
    inherit Component<Documentation.Types.Model,obj>(props)
    do
        ()

    override this.componentWillMount () =
        printfn "componentWillMount"
        **RequestProcessDocumentation |> dispatch <-- this is what I need**

    override this.render() =
        printfn "render"
        let (?) = Fable.Core.JsInterop.(?)

        div []
            [
                p [] [str(this.props.isLoading.ToString())]
                button [ OnClick (fun _ -> RequestProcessDocumentation |> dispatch ) ] [str("Click me")]
            ]

我怎样才能使用ofType函数创建它,所以我可以这样使用它:
  let pageHtml =
    function
    | Home -> Home.View.root model.home (HomeMsg >> dispatch)
    | Documentation -> documentation (DocumentationMsg >>

最佳答案

我在props中添加了dispatch函数:

[<Pojo>]
type Model = {
    isLoading:                  bool
    processDocumentation:       ProcessDocumentationDto
    valuesForFilterDropdown:    DropdownFilterValues
    scopeOfFilter:              ScopeOfFilter
    expandedMenuItemsIds:       string list
}

[<Pojo>]
type DocumentationProps = {
    model:      Model
    dispatch:   Msg -> unit
}

创建 View :
let root model dispatch =
  let inline documentation props = ofType<Documentation,_,_> props []

  let pageHtml =
    function
    | Home -> Home.View.root model.home (HomeMsg >> dispatch)
    | Documentation -> documentation { model.documentation with dispatch = (DocumentationMsg >> dispatch)}

然后我这样称呼它:
RequestProcessDocumentation |> this.props.dispatch

关于f# - 寓言React有状态组件上的Elmish调度,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49507597/

10-10 18:57