Created
November 14, 2018 00:20
-
-
Save jordangarcia/fa4604c2938d21d2aa269454e45a322b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const myFeature = createFeatureInstance('my-feature', { | |
getComponent(isEnabled, variables) { | |
return isEnabled | |
? ExtendedComponent | |
: StandardComponent | |
}, | |
fetchData(isEnabled, variables) { | |
return isEnabled | |
? getExtendedData() | |
: getStandardData() | |
}, | |
}) | |
// in routing | |
myFeature.fetchData() | |
// in component | |
render() { | |
const MyFeatureComponent = myFeature.getComponent() | |
return ( | |
<MyFeatureComponent {...propsToPass} /> | |
) | |
} | |
@OptimizelyFeature('my-feature', 'myFeature') | |
class MyFeatureWrapper extends React.Component { | |
constructor(props) { | |
super(props) | |
this.state = { | |
isLoading: false, | |
data: null, | |
} | |
} | |
componentDidMount() { | |
const { isEnabled } = this.props.myFeature | |
const dataPromise = isEnabled. | |
? fetchSimple() | |
? fetchDetailed() | |
dataPromise.then(data => { | |
this.setState({ | |
isLoading: false, | |
data, | |
}) | |
}) | |
} | |
render () { | |
const { isLoading, data } = this.state | |
const { isEnabled } = this.props.myFeature | |
if (isLoading) { | |
return <p>loading...</p> | |
} | |
return isEnabled | |
? <SimpleComponent data={data} /> | |
: <DetailedComponent data={data} /> | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment