-
Notifications
You must be signed in to change notification settings - Fork 37
feat (Hooks): Add useFeature hook #28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
9625b2f
to
2242064
Compare
2242064
to
318f094
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me, no big objections, just questions, mainly the one about how/when optimizely
is falsy.
I guess this means we drop support for React <16.9, is that right?
Allow consumers to avoid using class components (mostly in the interest of bundle size)
This could also be done by rewriting as a functional component that itself leverages built in React hooks for state management
Interesting, I wonder how much bundle size we could save by rewriting all our classes as hooks.
318f094
to
daa2eaf
Compare
777f4e2
to
dfccaf7
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, just a few small suggestions about docs and logging.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is going to be so great to use, @jamesopti and really change the speed at which we can ship / cleanup feature flags in the frontend. Requesting changes for some answers but nothing I see blocking thus far.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for all the info @jamesopti!
3e415a6
to
657a160
Compare
…at uses useFeature hook (#32) Summary Updates <OptimizelyFeature> to be a functional component that Also adds support for userId and userAttribute overrides. Addresses #30 Release Is this a breaking change? In theory the logic should behave the same, as #28 was designed to implement the same logic as <OptimizelyFeature>. Timing wise, when isServerSide is false, the state setting of the correct values of the feature/variables in useFeature happens in a second chained thennable, rather than the first. I dont think this should be considered a breaking change (and thus warrant a major release), but I wanted to point it out.
Summary
This PR adds a React Hook called
useFeature
, enabling feature usage (with variables) via the hooks interface.The API designed here is intended to be feature complete with the
<OptimizelyFeature>
component.Justification - Why add hooks?
Despite just being the new hotness in the React world, there are a couple good reasons to supporting the hooks paradigm in this SDK:
<OptimizelyFeature>
as a functional component that itself leverages built in React hooks for state managementTODO
clientReady
, which will indicate whether or not the client is ready.Example Usage
Directly from the README update:
arguments
feature : string
Key of the featureoptions : Object
autoUpdate : boolean
(optional) If true, this component will re-render in response to datafile or user changes. Default:false
.timeout : number
(optional) Rendering timeout as described in theOptimizelyProvider
section. Overrides any timeout set on the ancestorOptimizelyProvider
.overrides : Object
overrideUserId : string
(optional) Override the userId for calls toisFeatureEnabled
for this hook.overrideAttributes : optimizely.UserAttributes
(optional) Override the user attributes for calls toisFeatureEnabled
for this hook.AutoUpdate
A utility was added to help manage auto updates of the feature and it's variables in
autoUpdate.ts
. Eventually, this utility can be used in auseExperiment
hook.Additionally, the
useFeature
anduseExperiment
hooks could then handle all the state management and auto update logic for<OptimizelyFeature>
and<OptimizelyExperiment>
, allowing those to just be thin wrapper components.Test Plan
Minimal unit tests have been added to test the base case(s) here. Additional testing of the optional parameters will be added once this overall approach is agreed upon.
Note: Until we upgrade to React 16.9, there will be errors in the
hooks.spec.tsx
unit tests about not wrapping state setting test actions inact()
. React 16.9 introduces async support foract()
, which will allow us to wrap our invocation ofawait client.onReady()
Issues
Addresses #6