Skip to content

Instantly share code, notes, and snippets.

@simonpai
Created March 15, 2019 13:04
Show Gist options
  • Save simonpai/db159e3e7764b9ef8d766ad963ea5d14 to your computer and use it in GitHub Desktop.
Save simonpai/db159e3e7764b9ef8d766ad963ea5d14 to your computer and use it in GitHub Desktop.
useAPI
import { useContext } from 'react';
import { Context } from '../component/common/ApiProvider';
export default function useConfirm() {
return useContext(Context);
}
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