Skip to content

Instantly share code, notes, and snippets.

@samcorcos
Last active May 14, 2019 16:55
Show Gist options
  • Save samcorcos/fba8cc670180ed171a04f38eb171f70e to your computer and use it in GitHub Desktop.
Save samcorcos/fba8cc670180ed171a04f38eb171f70e to your computer and use it in GitHub Desktop.
import React from 'react'
import { firebase } from './firebase'
export const initialState = {
currentUser: {},
data: {}
}
export const Context = React.createContext(initialState)
/**
* This component is the Context Provider using the new React Context API that
* gives the app store state
* https://blog.bitsrc.io/react-context-api-a-replacement-for-redux-6e20790492b3
*/
export class Provider extends React.Component {
state = initialState
signOut = async () => {
await firebase.auth().signOut()
this.setState({ ...initialState })
}
set = (key, value) => {
this.setState({ [key]: value })
}
render () {
return (
<Context.Provider
value={{
// NOTE any actions defined in this component must be added to the value here as well
...this.state,
set: this.set,
signOut: this.signOut
}}>
{this.props.children}
</Context.Provider>
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment