reason-react icon indicating copy to clipboard operation
reason-react copied to clipboard

ReactDOMRe.render should return something

Open ccapndave opened this issue 5 years ago • 2 comments

ReactDOM.render actually returns an object (containing amongst other things a render function which can be used to update the properties of the rendered component at runtime). Because the type definition says the return type is unit, the compiler doesn't add a return statement which means the value is thrown away and can't be used by the rendering context.

This can be fixed with an extra function (with a different name so existing implementations don't break):

[@bs.val] [@bs.module "react-dom"] external renderWithReturn: (React.element, Dom.element) => 'a = "render";

ccapndave avatar Jul 30 '20 10:07 ccapndave

Do you have a use-case for that?

The issue is that ReactDOM.render returns whether:

  • a DOM node if your render a DOM component
  • null if you render a function component (which is the default with ReasonReact)
  • a reference to the component in case of component classes.

We cannot infer the return type from React.element.

Given how rarely this is used, I'd advice for users who really want to do that to write their own external like the one you added.

bloodyowl avatar Jul 30 '20 12:07 bloodyowl

To be honest, I thought I had a use case, but it turned out to be satisfied by simply re-rendering the component in the same container.

ccapndave avatar Jul 30 '20 12:07 ccapndave

Not sure how's worth adding an external like renderWithReturn considering that most APIs might change for React 17/18.

I will close now since it seems like isn't being needed, but happy to discuss further if more cases appear.

davesnx avatar Nov 23 '22 21:11 davesnx