简体   繁体   中英

Purescript Halogen Component, Input vs State

I want to make a Halogen Component where the component's input differs from its state. According to the guide for Halogen ( https://github.com/slamdata/purescript-halogen/blob/master/docs/5%20-%20Parent%20and%20child%20components.md#input-values ) this should be possible. I changed the example from the guide as follows

import Prelude
import Data.Int (decimal, toStringAs)
import Halogen as H
import Halogen.HTML as HH
import Halogen.HTML.Events as HHE

type Input = Int

type State = String

data Query a = HandleInput Input a

component :: forall m. H.Component HH.HTML Query Input Void m
component =
  H.component
    { initialState: id
    , render
    , eval
    , receiver: HHE.input HandleInput
    }
  where

  render :: State -> H.ComponentHTML Query
  render state =
    HH.div_
      [ HH.text "My input value is:"
      , HH.strong_ [ HH.text (show state) ]
      ]

  eval :: Query ~> H.ComponentDSL State Query Void m
  eval = case _ of
    HandleInput n next -> do
      oldN <- H.get
      when (oldN /= (toStringAs decimal n)) $ H.put $ toStringAs decimal n
      pure next

But then I get a compile error at the line with , receiver: HHE.input HandleInput

Could not match type

  String

with type

  Int

What am I doing wrong here?

initialState是使用输入值计算的,在您粘贴的代码中,该实现被实现为id ,因此它试图强制输入和状态类型匹配。

Changed line { initialState: id in { initialState: const initialState and added after where the following lines

initialState :: State
initialState = ""

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM