Skip to content

Conversation

lharries
Copy link
Contributor

@lharries lharries commented Feb 16, 2023

Adds a react library which can be used by create react apps and next js

API heavily inspired by posthog's react native app

Introduces the provider:
<PostHogProvider>...</PostHogProvider> which can either take an initiated posthog client, or an API key and options

And the following hooks:

  • usePostHog
  • useFeatureFlagVariantKey
  • useFeatureFlagEnabled
  • useFeatureFlagPayload
  • useFeatureFlags

TODO:

  • Update the playground to use these libraries
  • Add the feature flag playload hook
  • Bundle it properly with the main package e.g. `import { useFeatureFlag } from 'posthog-js/react' @benjackwhite
  • Get team experiments to review the feature flags API

After merging:

  • Update the docs

Checklist

@lharries lharries changed the title luke/react feat: react library Feb 16, 2023
@lharries lharries marked this pull request as draft February 16, 2023 18:21
@lharries
Copy link
Contributor Author

lharries commented Feb 16, 2023

@benjackwhite would be great to get your input on the bundling when you have a chance. Feel free to ignore the files outside of the '/react' folder - I'll clean these up tomorrow

@lharries
Copy link
Contributor Author

@lharries
Copy link
Contributor Author

lharries commented Feb 17, 2023

The react native API has the hooks getFeatureFlag which is the same as isFeatureEnabled in the js library. And getFeatureFlags which is the same as the onFeatureFlags callback

However, with the payload function too I wonder if getFeatureFlag will be confusing for the JS SDK as I would expect that to return the payload. In this PR I've proposed staying close to the JS file naming:

  • useFeatureFlag - returns the result of useFeatureFlagPayload if it exists, otherwise the result of useFeatureFlagEnabled
  • useFeatureFlagEnabled - which uses isFeatureEnabled
  • useFeatureFlagPayload - which uses getFeatureFlagPayload
  • useFeatureFlags - which returns the callback from onFeatureFlags

@neilkakkar @EDsCODE and @liyiy what API do you want for the react feature flags?

@lharries
Copy link
Contributor Author

lharries commented Feb 27, 2023

Mind explaining re: useFeatureFlag, why is returning the variant key more confusing that returning the payload or the variant sometimes? 🤔

I think it links to the issues that Ben and I were mentioning

For the useFeatureFlag, if I was calling it and had a payload I'd expect it to return a payload - regardless of whether it's a multivariate or a boolean

@neilkakkar
Copy link
Contributor

neilkakkar commented Feb 27, 2023

Hmm, removing it means we end up missing a big piece of usefulness here, since right now there is no equivalent of posthog.getFeatureFlag(). You can somewhat get it from getActiveFeatureFlags, but that's missing the $feature_flag_called calls for experiments & audit.

I'll leave it up to you though!

If it helps, you can leave out useFeatureFlag, and instead have useFeatureFlagKey which maps to getFeatureFlag() ? That seems like a common ground?

@lharries
Copy link
Contributor Author

Hmm, removing it means we end up missing a big piece of usefulness here, since right now there is no equivalent of posthog.getFeatureFlag(). You can somewhat get it from getActiveFeatureFlags, but that's missing the $feature_flag_called calls for experiments & audit.

Good point. I like the idea of a useFeatureFlagKey function or a useFeatureFlagVariantId however getFeatureFlag seems to return either a boolean or a string. I would have expected the key to always be a string?

@neilkakkar
Copy link
Contributor

neilkakkar commented Feb 27, 2023

Yeah, useFeatureFlagVariantKey is more accurate, instead of FeatureFlagKey which means the flag name that you're passing in 😓

The boolean is the variant key for a flag that has no variants (and responds with true), which exists for backwards compatibility iirc, but yeah for a flag with no variants you really shouldn't be calling getFeatureflag()

@lharries
Copy link
Contributor Author

The boolean is the variant key for a flag that has no variants (and responds with true), which exists for backwards compatibility iirc, but yeah for a flag with no variants you really shouldn't be calling getFeatureflag()

I've just tried it and for a boolean value getFeatureFlag returns true if it's a boolean type. I'd have thought that the variantKey would be a string? I'm skeptical we'd want to return true/false for the VariantKey and have boolean in the type signature

I'd be keen to merge what we have so far and then in parallel I think you're still working through the proposal ben mentioned to streamline it to just getFeatureFlagPayload and getFeatureFlagVariantKey (unless you have reached a decision)

@lharries
Copy link
Contributor Author

@neilkakkar if you agree, would you mind giving a final review of the PR including a check of the new removing event listener function I added

@neilkakkar
Copy link
Contributor

Didn't manually try it out, but I'm hoping you have! Code looks good

@neilkakkar neilkakkar added the bump minor Bump minor version when this PR gets merged label Feb 28, 2023
@github-actions
Copy link

github-actions bot commented Feb 28, 2023

Size Change: +896 B (0%)

Total Size: 2.28 MB

Filename Size Change
dist/array.full.js 151 kB +224 B (0%)
dist/array.js 92.9 kB +224 B (0%)
dist/es.js 92.7 kB +224 B (0%)
dist/module.js 93 kB +224 B (0%)
ℹ️ View Unchanged
Filename Size
dist/recorder-v2.js 92.5 kB
dist/recorder.js 58.3 kB
lib/rrweb/dist/plugins/console-record.js 26.6 kB
lib/rrweb/dist/plugins/console-record.min.js 10.8 kB
lib/rrweb/dist/plugins/console-replay.js 12.7 kB
lib/rrweb/dist/plugins/console-replay.min.js 5.72 kB
lib/rrweb/dist/plugins/sequential-id-record.js 910 B
lib/rrweb/dist/plugins/sequential-id-record.min.js 417 B
lib/rrweb/dist/plugins/sequential-id-replay.js 1.24 kB
lib/rrweb/dist/plugins/sequential-id-replay.min.js 578 B
lib/rrweb/dist/record/rrweb-record-pack.js 31.4 kB
lib/rrweb/dist/record/rrweb-record-pack.min.js 10.7 kB
lib/rrweb/dist/record/rrweb-record.js 140 kB
lib/rrweb/dist/record/rrweb-record.min.js 51.7 kB
lib/rrweb/dist/replay/rrweb-replay-unpack.js 188 kB
lib/rrweb/dist/replay/rrweb-replay-unpack.min.js 71.1 kB
lib/rrweb/dist/replay/rrweb-replay.js 172 kB
lib/rrweb/dist/replay/rrweb-replay.min.js 66.4 kB
lib/rrweb/dist/rrweb-all.js 356 kB
lib/rrweb/dist/rrweb-all.min.js 133 kB
lib/rrweb/dist/rrweb.js 302 kB
lib/rrweb/dist/rrweb.min.js 116 kB
lib/rrweb/es/rrweb/ext/base64-arraybuffer/dist/base64-arraybuffer.es5.js 1.94 kB
lib/rrweb/es/rrweb/ext/mitt/dist/mitt.es.js 1.99 kB

compressed-size-action

@lharries
Copy link
Contributor Author

@neilkakkar based on our discussion I agree and it makes sense to add useFeatureFlagVariantKey - I've added that here: 7376f32 can you confirm you're happy with it?

@lharries lharries merged commit 4a2a7aa into master Feb 28, 2023
@lharries lharries deleted the luke/react branch February 28, 2023 14:39
@lharries
Copy link
Contributor Author

@ivanagas and @pjhul we now have a react library! Would be awesome to get your help with updating the docs and older tutorials. I'll take a crack at some of them later too

@lharries
Copy link
Contributor Author

@joethreepwood could be good to have some marketing support around this once we've updated the docs. Main benefit for users it that it makes implementation much easier/quicker.

@joethreepwood
Copy link

Think it can go in the Changelog this week, or is this going to drift to next?

@lharries
Copy link
Contributor Author

I'll update the docs to a good enough place by EOD that it can go in this week

@lharries
Copy link
Contributor Author

lharries commented Mar 1, 2023

@joethreepwood as discussed @pauldambra has a few more tweaks he wants to do so likely this will be next week instead

@joethreepwood
Copy link

@joethreepwood as discussed @pauldambra has a few more tweaks he wants to do so likely this will be next week instead

I thought that was in reference to Dashboard templates, or is that the case here too?

@lharries
Copy link
Contributor Author

lharries commented Mar 1, 2023

I had the wrong tab open - my fault! Yep this is ready to go when the docs merge later today. Dashboard templates will need another week

@defunctzombie
Copy link

I know its been a while since this was implemented - but I wanted to drop a note that I was recently tripped up by the decision to not set the initial state of the flag:

https://github.com/PostHog/posthog-js/blob/main/react/src/hooks/useFeatureFlagEnabled.ts#L7-L9

This results in users getting undefined and then getting the flag value - even when the flag value is known at startup. I stumbled upon this because of some undesired thrashing and rendering in our app.

IMO the workarounds should be on nextjs users or SSR users to maybe have a separate set of functions - and for everyone that does not care about SSR to not be hit with this behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bump minor Bump minor version when this PR gets merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants