Skip to content
This repository has been archived by the owner on Dec 7, 2022. It is now read-only.

Children render callback #85

Open
HHogg opened this issue Oct 24, 2018 · 4 comments
Open

Children render callback #85

HHogg opened this issue Oct 24, 2018 · 4 comments

Comments

@HHogg
Copy link

HHogg commented Oct 24, 2018

Hi, would you be open to a PR that allows the render callback to be passed in as the children prop? This would allow people to keep the render logic together inside the render function.

The API I imagine it being like is something like...

render() {
  return (
    <FeatureFlag key="Feature1">
      { (value) => value 
        ? <Feature1Component /> 
        : <FallbackComponent />
      }
    </FeatureFlag>
  );
}

And this would also work for multivariant flags too...

render() {
  return (
    <FeatureFlag key="Feature1">
      { (value) => {
        switch (value) {
          case 1: return <Feature1Case1 />
          case 2: return <Feature1Case2 />
          case 3: return <Feature1Case3 />
          default: return <FallbackComponent />
        }
      } }
    </FeatureFlag>
  );
}

As FeatureFlag doesn't support any children at the moment, then it would not be a breaking change and could just be given the value.

I guess the change would be here, with something like ...

if (children) {
  return children(flagValue);
}
@sethbattin
Copy link
Contributor

The proposal sounds fine, and the syntax change is definitely preferable.

I'm a little bit skeptical of being able to implement it with perfect back-compat. But no need to speculate; we might as well make the attempt. I think I would still lean toward calling it a major version number just to be slightly safer. It might simplify things to not attempt to keep it compatible, and just warn profusely about upgrading. Perhaps providing a codemod would be feasible.

There would be an existing use case that'd be a little tricky; it might not be feasible with a single-argument children-function. We have a scenario where rendering happens before any settings are loaded. Currently that is handled by initialRenderCallback prop. It would be awful to have to check type of a falsey value, say to different null/false/undefined. So perhaps that just implies a more complicated function signature for the children function. 🤷‍♂️ I don't know; see what you can do.

Obviously it would need doc updates too.

@HHogg
Copy link
Author

HHogg commented Oct 24, 2018

Thanks for the feedback @sethbattin, I had not thought about the case before initial flags had been received. Is this what the checkFeatureFlagComplete flag is for inside FeatureFlagRenderer?

If so, perhaps we can pass that down also, something like.

render() {
  return (
    <FeatureFlag key="Feature1">
      { ({ flagInitialised, flagValue }) => {
        switch (flagValue) {
          case 1: return <Feature1Case1 />
          case 2: return <Feature1Case2 />
          case 3: return <Feature1Case3 />
          default: return flagInitialised  
            ? <FallbackComponent /> 
            : <InitialComponent />
        }
      } }
    </FeatureFlag>
  );
}

@andrewdushane
Copy link
Contributor

andrewdushane commented Oct 24, 2018 via email

@HHogg
Copy link
Author

HHogg commented Oct 29, 2018

That's understandable @andrewdushane, trying to provide 2 completely different ways to use the component does sound very messy. The reasons I prefer this, is that that it feels like a more familiar pattern, similar to other behavioural components like the new context API and React Router.

I'm happy to submit a PR that implements this alongside or instead of the existing API, or just create my own abstraction that allows me to do this. @sethbattin @andrewdushane what do you think is best, and would be happier with?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants