Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add useSnackbar hook #83

Merged
merged 2 commits into from
Mar 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ export interface withSnackbarProps {
export function withSnackbar<P extends withSnackbarProps>(component: React.ComponentType<P>):
React.ComponentClass<Omit<P, keyof withSnackbarProps>> & { WrappedComponent: React.ComponentType<P> };


export function useSnackbar(): InjectedNotistackProps;

export type ClassNameMap<ClassKey extends string = string> = Record<ClassKey, string>;

// all material-ui props, including class keys for notistack and material-ui with additional notistack props
export interface SnackbarProviderProps extends Omit<SnackbarProps, 'open' | 'message' | 'classes'> {
classes?: Partial<ClassNameMap<CombinedClassKey>>;
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { default as SnackbarProvider } from './SnackbarProvider';
export { default as withSnackbar } from './withSnackbar';
export { default as useSnackbar } from './useSnackbar';
17 changes: 17 additions & 0 deletions src/useSnackbar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { useContext } from 'react';
import { SnackbarContext, SnackbarContextNext } from './SnackbarContext';

const useSnackbar = () => {
const handlePresentSnackbar = useContext(SnackbarContext);
const {
handleEnqueueSnackbar,
handleCloseSnackbar,
} = useContext(SnackbarContextNext);
return {
onPresentSnackbar: handlePresentSnackbar,
enqueueSnackbar: handleEnqueueSnackbar,
closeSnackbar: handleCloseSnackbar,
};
};

export default useSnackbar;