Last active
August 10, 2018 02:15
-
-
Save wnqueiroz/d5a68ac6c52b5541c304c9cfda51eb1e to your computer and use it in GitHub Desktop.
React Context: App.jsx with loading context api setup
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
import React, { Component, Fragment } from 'react' | |
import Departments from './components/Departments' | |
import Users from './components/Users' | |
import Loading from './components/Loading' | |
import './index.css' | |
const LoadingContext = React.createContext({ | |
loading: false, | |
message: null, | |
showLoading: message => { }, | |
hideLoading: () => { } | |
}) | |
class App extends Component { | |
state = { | |
loading: false, | |
message: null | |
} | |
showLoading = message => this.setState({ | |
loading: true, | |
message | |
}) | |
hideLoading = () => this.setState({ loading: false }) | |
render() { | |
const { showLoading, hideLoading } = this | |
const value = { | |
...this.state, | |
showLoading, | |
hideLoading | |
} | |
return ( | |
<LoadingContext.Provider value={value}> | |
<LoadingContext.Consumer> | |
{ | |
({ showLoading, hideLoading, loading, message }) => ( | |
<Fragment> | |
<Users {...{ showLoading, hideLoading }} /> | |
<Departments {...{ showLoading, hideLoading }} /> | |
<Loading {...{ loading, message }} /> | |
</Fragment> | |
) | |
} | |
</LoadingContext.Consumer> | |
</LoadingContext.Provider> | |
) | |
} | |
} | |
export default App |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment