A thin wrapper over react-basic-hooks that provides a simplified API for creating React components without Effect boilerplate.
spago install yoga-reactimport Yoga.React (component)
-- Create a component at the top-level
myButton :: { label :: String, onClick :: Effect Unit } -> JSX
myButton = component "MyButton" \props -> React.do
pure $ button { onClick: props.onClick } [ text props.label ]component
:: forall hooks props
. String
-> (props -> Hooks.Render Unit hooks JSX)
-> (props -> JSX)Creates a component using unsafePerformEffect internally. This is safe only when used at the top-level as a module-level binding.
purslint linter has a rule to catch this mistake.
-- Top-level binding
greeting = component "Greeting" \props -> React.do
pure $ div {} [ text $ "Hello " <> props.name ]-- Inside a function - will break React!
makeComponent name =
component name \_ -> pure mempty -- ERROR: purslint will catch thisCreating components with react-basic-hooks requires wrapping in Effect:
-- Traditional approach
myComp :: Effect ({ } -> JSX)
myComp = Hooks.component "MyComp" \props -> React.do
pure $ text "Hello"
-- Then you need to run the Effect somewhere
main = do
comp <- myComp
-- ...With yoga-react:
-- Simplified approach
myComp :: { } -> JSX
myComp = component "MyComp" \props -> React.do
pure $ text "Hello"
-- Use it directly, no Effect to runThis is safe because React components should only be created once at module initialization time anyway.
WTFPL