purescript-contrib/purescript-react

Can't use different `ReactRender` types in render

natefaubion opened this issue · 1 comments

The polymorphism over render with ReactRender lets you use different return types, but it's fixed for the class, which means you can't return different types on different ticks of render. I should be able to return a String in one render tick, and a ReactElement in another tick. The correct type I think would be:

{ ... render :: forall r. ReactRender r. ReactThis props state => Eff ... r }

Edit: This is actually wrong, since it means render can't fix the type.
But this higher-ranked type is problematic. I think instead we should just define coercions to ReactElement, since these can actually be used anywhere and not just the root of a render.

fromString :: String -> ReactElement
fromInt :: Int -> ReactElement
fromNumber :: Number -> ReactElement
fromArray :: Array ReactElement -> ReactElement

I think if we wanted to keep ReactRender, it would be better to actually give it a member, so that it can be overloaded for arbitrary PureScript types.

class ReactRender a where
  toElement :: a -> ReactElement

instance reactRenderString :: ReactRender String where
  toElement = unsafeCoerce :: String -> ReactElement
...

Where the "native" element types are implemented via unsafeCoerce. We should still fix the type of render to return a ReactElement. Users should then opt in with toElement.