Created
March 15, 2019 13:04
-
-
Save simonpai/db159e3e7764b9ef8d766ad963ea5d14 to your computer and use it in GitHub Desktop.
useAPI
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 { useContext } from 'react'; | |
import { Context } from '../component/common/ApiProvider'; | |
export default function useConfirm() { | |
return useContext(Context); | |
} |
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 from 'react'; | |
import PropTypes from 'prop-types'; | |
import { Provider, connect } from 'react-redux'; | |
import { api } from '../../util/stores'; | |
export const Context = React.createContext(); | |
function mapDispatchToProps(dispatch, {action}) { | |
return { | |
api: api(action, dispatch) | |
} | |
} | |
const ApiContextProvider = connect(undefined, mapDispatchToProps)(({api, children}) => ( | |
<Context.Provider value={api}> | |
{children} | |
</Context.Provider> | |
)); | |
function ApiProvider({store, action, children}) { | |
return ( | |
<Provider store={store}> | |
<ApiContextProvider action={action}> | |
{children} | |
</ApiContextProvider> | |
</Provider> | |
); | |
} | |
ApiProvider.propTypes = { | |
store: PropTypes.object.isRequired, | |
action: PropTypes.object.isRequired | |
}; | |
export default ApiProvider; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment