如果我有一个祖父母,一个子组件和一个孙子组件,祖父母可以请求孩子的状态吗?我试过像这里一样使用“请求”,但是当你请求一个也有自己的孩子的孩子的状态时,类型不匹配。当我请求没有孩子的孩子的状态时,指南中的示例可以正常工作。
错误是:
Could not match type
Query
with type
Coproduct (Coproduct Query (ChildF AnswerSlot Query)) Query
如果我有一个祖父母,一个子组件和一个孙子组件,祖父母可以请求孩子的状态吗?我试过像这里一样使用“请求”,但是当你请求一个也有自己的孩子的孩子的状态时,类型不匹配。当我请求没有孩子的孩子的状态时,指南中的示例可以正常工作。
错误是:
Could not match type
Query
with type
Coproduct (Coproduct Query (ChildF AnswerSlot Query)) Query
当然是。您可能只是left
在对孩子的查询中遗漏了 a - 这是必需的,因为孩子的查询代数将Coproduct f (ChildF p f')
具有自己的孩子的形式。
相应地,您也可以从祖父母那里查询孙子,方法是使用right
和构造ChildF
带有适当值的孙子。
我整理了一个查询孩子和孙子的人为示例,希望可以使事情更清楚:
module Main where
import Prelude
import Data.Functor.Coproduct (Coproduct, left, right)
import Data.Maybe (Maybe(..), fromMaybe)
import Debug.Trace (traceA) -- from purescript-debug
import Halogen as H
import Halogen.HTML as HH
--------------------------------------------------------------------------------
type GrandState = Unit
data GrandQuery a = AskGrandChild (String -> a)
grandchild :: forall g. H.Component GrandState GrandQuery g
grandchild = H.component { render, eval }
where
render :: GrandState -> H.ComponentHTML GrandQuery
render _ = HH.div_ []
eval :: GrandQuery ~> H.ComponentDSL GrandState GrandQuery g
eval (AskGrandChild k) = pure $ k "Hello from grandchild"
--------------------------------------------------------------------------------
type ChildState = Unit
data ChildQuery a = AskChild (String -> a)
type GrandSlot = Unit
type ChildState' g = H.ParentState ChildState GrandState ChildQuery GrandQuery g GrandSlot
type ChildQuery' = Coproduct ChildQuery (H.ChildF GrandSlot GrandQuery)
child :: forall g. Functor g => H.Component (ChildState' g) ChildQuery' g
child = H.parentComponent { render, eval, peek: Nothing }
where
render :: ChildState -> H.ParentHTML GrandState ChildQuery GrandQuery g GrandSlot
render _ = HH.slot unit \_ -> { component: grandchild, initialState: unit }
eval :: ChildQuery ~> H.ParentDSL ChildState GrandState ChildQuery GrandQuery g GrandSlot
eval (AskChild k) = pure $ k "Hello from child"
--------------------------------------------------------------------------------
type ParentState = Unit
data ParentQuery a = Something a
type ChildSlot = Unit
type ParentState' g = H.ParentState ParentState (ChildState' g) ParentQuery ChildQuery' g ChildSlot
type ParentQuery' = Coproduct ParentQuery (H.ChildF ChildSlot ChildQuery')
parent :: forall g. Functor g => H.Component (ParentState' g) ParentQuery' g
parent = H.parentComponent { render, eval, peek: Nothing }
where
render :: ParentState -> H.ParentHTML (ChildState' g) ParentQuery ChildQuery' g ChildSlot
render _ = HH.slot unit \_ -> { component: child, initialState: H.parentState unit }
eval :: ParentQuery ~> H.ParentDSL ParentState (ChildState' g) ParentQuery ChildQuery' g ChildSlot
eval (Something next) = do
-- note the `left` here
childAnswer <- H.query unit $ left $ H.request AskChild
traceA $ fromMaybe "child not found" $ childAnswer
grandAnswer <- H.query unit $ right $ H.ChildF unit $ H.request AskGrandChild
traceA $ fromMaybe "grandchild not found" $ grandAnswer
pure next