purescript-react/purescript-react-basic

`unsafeElement` function proposal

starper opened this issue · 2 comments

Currently element function forces you to use records as props, but sometimes records are not the best choice, because they are too strict. For example, let's say we have a react component that has two reqired props and ten optional ones. Some default values will be used when optional props are not set. In my opinion something like Options will suit better here. The problem is that there is no way to transform Options into records, only into Foreign. So, as a solution for this problem I propose this function

unsafeElement :: ReactComponent Foreign -> Foreign -> JSX

It is supposed to be used inside a wrapper component with types that can be transformed into Foreign, such as Options or instances of type class Encode. I think it should be marked as unsafe, because, when using it, it will be quite easy to mess things up if you are not careful enough.

Actually, maybe something like

unsafeElement :: forall a. ReactComponent a -> a -> JSX

would be better. This way element function could be implemented like this

element :: forall props. ReactComponent { | props } -> { | props } -> JSX
element = unsafeElement

Writing your own type for element and defining it to call the existing element using unsafeCoerce wouldn't be less safe than this proposal. I think that's probably how this should work for now, and if you find a custom wrapper like that to be useful we can consider merging it as an alternative, safe function.