Skip to content
This repository was archived by the owner on Sep 28, 2023. It is now read-only.
This repository was archived by the owner on Sep 28, 2023. It is now read-only.

State of child-components is not updated if OptimizelyProvider's props are changed. #11

@VasylBoyko

Description

@VasylBoyko

There is no possibility to update datafile or userId of OptimizelyProvider.
Use cases: if user is logged in, datafile is changed.

const optimizelyRevision = 0;
class App extends Component {
    constructor(props) {
        super(props);
        this.state = {
            optimizely: null
        };
    }

    componentDidMount() {
        this.getOptimizelyData();
        //Check for updates each 5 min
        setInterval(this.getOptimizelyData, 5 * 60 * 1000);
    }

    getOptimizelyData = () => {
        fetch(optimizelyDatafileUrl).then(resp => resp.json()).then((optimizelyDatafile) => {
            const revision = parseInt(optimizelyDatafile.revision, 10);
            if (!Number.isNaN(revision) && revision > optimizelyRevision) {

                // Newer Datafile available!

                optimizelyRevision = revision;
                const optimizely = optimizelySDK.createInstance({
                    datafile: optimizelyDatafile,
                });

                this.setState({
                    optimizely
                });
            }
        });
    }

    onLogin = (userId) {
        this.setState({
            userId
        });
    }

    render () {
        const { optimizely, userId } = this.state;

        return optimizely && <OptimizelyProvider optimizely={optimizely} userId={userId}>
            <OptimizelyFeature feature="FEATURE_FLAG">
                {featureEnabled => (featureEnabled
                    ? <div>Enabled</div>
                    : <div>Disabled</div>
                )}
            </OptimizelyFeature>
        </OptimizelyProvider>
    }
}

According to https://docs.developers.optimizely.com/rollouts/docs/instantiate-a-client

 Any time you retrieve an updated datafile, just re-instantiate the same client.

I've re-instantiated client but value of featureEnabled is not changed.

imho, OptimizelyFeature (and other...) component should have something like that:

static getDerivedStateFromProps(nextProps, prevState) {
        const { feature, optimizely } = nextProps;
        let newState = null;

        if (prevState.optimizely !== optimizely) {
            const isEnabled = optimizely.isFeatureEnabled(feature);
            const variables = optimizely.getFeatureVariables(feature);
            newState = {
                canRender: true,
                isEnabled,
                variables,
                optimizely
            };
        }
        return newState;
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions