我需要使用带有elmish dispatch的Fable-React有状态组件。我无法弄清楚如何创建它。
我正在使用此项目模板: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 >>
答案 0 :(得分:2)
我将调度功能添加到道具:
[<Pojo>]
type Model = {
isLoading: bool
processDocumentation: ProcessDocumentationDto
valuesForFilterDropdown: DropdownFilterValues
scopeOfFilter: ScopeOfFilter
expandedMenuItemsIds: string list
}
[<Pojo>]
type DocumentationProps = {
model: Model
dispatch: Msg -> unit
}
创建视图:
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