Last active
November 7, 2018 13:09
-
-
Save tortillaj/384a74a7708206085549122700d286dc to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import React from 'react' | |
import { api } from 'ducks/user' | |
const MultiplePatronContext = React.createContext({ | |
isMultiplePatronEnabled: false, | |
error: '', | |
}) | |
export class MultiplePatronProvider extends React.Component { | |
state = { isMultiplePatronEnabled: false } | |
async componentDidMount() { | |
try { | |
const response = await api.fetchMultiplePatronsEnabled().then(res => res.data) | |
this.setState({ isMultiplePatronEnabled: response.enabled }) | |
} catch (ex) { | |
this.setState({ error: ex.message }) | |
} | |
} | |
render() { | |
return ( | |
<MultiplePatronContext.Provider value={this.state.multiplePatronEnabled}> | |
{this.props.children} | |
</MultiplePatronContext.Provider> | |
) | |
} | |
} | |
export const MultiplePatron = MultiplePatronContext.Consumer | |
// | |
// | |
// usage | |
// | |
// | |
<MultiplePatronProvider> | |
... | |
<MultiplePatron> | |
{isMultiplePatronEnabled => ( | |
.... | |
)} | |
</MultiplePatron> | |
... | |
</MultiplePatronProvider> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment