GWTReact / gwt-react

GWT Java bindings for React
MIT License
91 stars 17 forks source link

Creating functional components using static method references doesn't work correctly #19

Open pstockley opened 6 years ago

pstockley commented 6 years ago

GWT currently creates a new function each time a method reference is used, even if the method is static. This causes react to re-render the functional component every time as it sees it as a different functional component.

We should remove method references as a recommended way of defining functional components.

ivmarkov commented 6 years ago

We stumbled on this as well, however I don't think this is a bug. It is more of a usage pattern, and it has a very easy workaround. Here's how we define and use stateless components: public static final StatelessComponent<Props> TYPE = MyComponent::render;

... where MyComponent::render is: private static ReactElement<?, ?> render(Props props) {

and finally, we use it like this: return React.createElement(MyComponent.TYPE, props);

The key here is the final static TYPE field member. Since it is final static, it is created only once, hence the function wrapping the method reference is also created once only.

ivmarkov commented 6 years ago

I suggest we just document the pattern in the README.md file.