Skip to content
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

React integration for feature flags #1997

Closed
macobo opened this issue Oct 23, 2020 · 2 comments · Fixed by PostHog/posthog-js#154
Closed

React integration for feature flags #1997

macobo opened this issue Oct 23, 2020 · 2 comments · Fixed by PostHog/posthog-js#154
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@macobo
Copy link
Contributor

macobo commented Oct 23, 2020

Is your feature request related to a problem?

Our user needed to roll his own solution for refreshing feature flags: PostHog/posthog-js#100

We can provide tools to help them.

Describe the solution you'd like

Provide a hook, component or some other standardized way to use feature flags in react code which reacts (hah) to feature flags changing.

Stretch: provide ways to configure how/when feature flags are refreshed.

Describe alternatives you've considered

Additional context

We already have a partial solution in place in frontend/src/lib/logic/featureFlagLogic.js - but this is kea.js specific

Thank you for your feature request – we love each and every one!

@macobo macobo added the enhancement New feature or request label Oct 23, 2020
@mariusandra
Copy link
Collaborator

Copy/pasting the kea-specific code here for others to take inspiration from:

// kea posthog feature flags
import { kea } from 'kea'

export const featureFlagLogic = kea({
    actions: {
        posthogFound: (posthog) => ({ posthog }),
        setFeatureFlags: (featureFlags) => ({ featureFlags }),
    },

    reducers: {
        featureFlags: [
            {},
            {
                setFeatureFlags: (state, { featureFlags }) => {
                    const flags = {}
                    for (const flag of featureFlags) {
                        flags[flag] = true
                    }
                    return flags
                },
            },
        ],
    },

    listeners: ({ actions }) => ({
        posthogFound: ({ posthog }) => {
            posthog.onFeatureFlags(actions.setFeatureFlags)
        },
    }),

    events: ({ actions, cache }) => ({
        afterMount: () => {
            if (typeof window !== 'undefined') {
                if (window.posthog) {
                    actions.posthogFound(window.posthog)
                } else {
                    // check every 300ms if posthog is now there
                    cache.posthogInterval = window.setInterval(() => {
                        if (window.posthog) {
                            actions.posthogFound(window.posthog)
                            window.clearInterval(cache.posthogInterval)
                        }
                    }, 300)
                }
            }
        },
        beforeUnmount: () => {
            if (typeof window !== 'undefined') {
                window.clearInterval(cache.posthogInterval)
            }
        },
    }),
})

The concepts should translate to the react hook syntax with a bit of work.

@mariusandra mariusandra added the good first issue Good for newcomers label Oct 23, 2020
@ghost
Copy link

ghost commented Dec 9, 2020

Hi guys, I've created a pull request against the posthog-js repo which aims to close this ticket. Please let me know your thoughts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants