Skip to content

chore (Hooks): Move common client ready logic to a separate function #34

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

Merged
merged 3 commits into from
Mar 17, 2020

Conversation

jamesopti
Copy link
Contributor

Summary

In preparation to introduce a useExperiment hook, this PR moves the logic for waiting for the client to become ready, setting initial state, and configuring autoupdate to a shared function, which is passed to useEffect

There are a lot of variables that need to be passed, but I think this is much preferable to duplicating the somewhat intricate & nuanced logic that exists there.

Test Plan

Existing unit tests cover this functionality, and more will be added with the introduction of useExperiment

Copy link
Contributor

@mjc1283 mjc1283 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few suggestions, but LGTM either way.

src/hooks.ts Outdated
Comment on lines 143 to 151
const [data, setData] = useState<UseFeatureState>(() => {
if (isServerSide) {
return getCurrentValues();
}
return { isEnabled: false, variables: {} };
});

const [clientReady, setClientReady] = useState(isServerSide ? true : false);
const [didTimeout, setDidTimeout] = useState(false);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To reduce the number of arguments going around, is it possible to collapse all this into one useState call returning an object with all these things? Then set different parts of it as needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I initially thought this would be a hassle since the useState set state method doesnt extend by default, making it necessary to return the entire state object.

However, I think you're right that it's worth it here, and it makes the typing simpler too.

Set state calls now look like this:

setState((state: HookState) => Object.assign({}, state, { didTimeout: true }));

@jamesopti
Copy link
Contributor Author

@mjc1283 @circAssimilate @pauloptimizely - Gonna merge this now to proceed but happy to address any feedback in follow up PRs!

@jamesopti jamesopti merged commit e2cf17c into master Mar 17, 2020
@jamesopti jamesopti deleted the james/make_hook_reusable branch March 17, 2020 19:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants