GWTReact / gwt-react

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

Provide an alternative to JSX conditional rendering #18

Closed pstockley closed 6 years ago

pstockley commented 6 years ago

In JSX you can do conditional rendering of components inline as follows:

{some_condition && <Component to render />}

Currently, you would need to move this code to a separate method e.g.

Element<?,?> renderComponentIfConditionSet() {
    if (some_condition)
    return React.CreateElement(ComponentToRender.class)
     else
        return null
}

We should introduce a helper function to support this kind of conditional rendering inline e.g

GwtReact.renderWhen(some_condition,  component to render)
ivmarkov commented 6 years ago

The problem with your suggestion is that <component-to-render> will be evaluated even if <some-condition> is falsy, because <component-to-render> is a function parameter. ...unlike the JavaScript "truthy" syntax, which will evaluate <component-to-render> only if <some-condition> is truthy.

The closest thing to JavaScript's truthy syntax we've come up with is the standard Java ?: "expression if" statement. It is a bit longer than the equivalent JS truthy syntrax, but not that long at all. We just do: <some-condition>? <component-to-render>: null

ivmarkov commented 6 years ago

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