Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. import React, { useReducer } from "react";
  2. const ModalContext = React.createContext({});
  3.  
  4. const reducer = (state, { component }) => {
  5. return { ...state, component };
  6. };
  7.  
  8. const ModalProvider = ({ children }) => {
  9. const showModal = component => {
  10. return dispatch({ component });
  11. };
  12.  
  13. const hideModal = () => {
  14. return dispatch({ ...state, component: null });
  15. };
  16.  
  17. const [state, dispatch] = useReducer(reducer, {
  18. component: null,
  19. showModal,
  20. hideModal
  21. });
  22.  
  23. return (
  24. <ModalContext.Provider
  25. value={{
  26. state,
  27. showModal
  28. }}
  29. >
  30. {children}
  31. </ModalContext.Provider>
  32. );
  33. };
  34.  
  35. const ModalConsumer = ModalContext.Consumer;
  36.  
  37. export { ModalProvider, ModalContext, ModalConsumer };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement