Skip to content

Instantly share code, notes, and snippets.

@capJavert
Created April 2, 2019 20:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save capJavert/6e4ca16ddee23925da96909b415aca4d to your computer and use it in GitHub Desktop.
Save capJavert/6e4ca16ddee23925da96909b415aca4d to your computer and use it in GitHub Desktop.
HOC for injecting config props after firebase remote config async call is completed
import React from 'react'
import firebase from 'react-native-firebase'
const withRemoteConfig = (configValues = []) => Component => (
class EnhancedComponent extends React.Component {
state = {
config: {}
}
componentDidMount = async () => {
if (!configValues.length) {
return
}
const snapshot = await firebase.config().getValues(configValues)
if (snapshot) {
const config = Object.keys(snapshot).reduce((acc, key) => {
acc[key] = snapshot[key].val()
return acc
}, {})
this.setState({ config })
}
}
render = () => {
const { config } = this.state
return (
<Component
{...this.props}
config={config}
/>
)
}
}
)
export default withRemoteConfig
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment