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

[WIP] add useTreatment hook

Open besh opened this issue 4 years ago • 0 comments

React SDK

What did you accomplish?

Added a useTreatment hook that takes in a single split. Addresses https://github.com/splitio/react-client/issues/69

How do we test the changes introduced in this PR?

Added tests to verify the new hook. You can test locally via npm-link or yalc in a project repo. One test will fail until I modify further (hence WIP).

Extra Notes

Initially, I wanted to create a useTreatment hook that used client.getTreatment. However, the existing useTreatments hook is using getTreatmentsWithConfig under the hood, so I chose to follow the existing convention. Shouldn't it instead be using getTreatements and then have a useTreatementsWithConfig hook that uses getTreatmentsWithConfig? I understand that at this point, it would be a version breaking change.

The end API that I'd like is

const treatment = useTreatment('my_split');

if (treatment === 'on') {
  // do something
}

However, because I'm following the existing convention of the useTreatments hook, the resulting usage is instead

const treatmentResult = useTreatment('my_split');

if (treatmentResult.treatment === 'on') {
  // do something
}

Would you be okay if I added two hooks?

  • useTreatment -> uses client.getTreatment
  • useTreatmentWithConfig -> uses client.getTreatmentWithConfig

These additions wouldn't be version breaking as they are additive, but it wouldn't follow the existing convention set in place. Looking for feedback and thoughts here!

besh avatar Aug 31 '21 02:08 besh