PorterFormContextProvider.tsx 825 B

123456789101112131415161718192021222324252627282930313233
  1. import React, { createContext, useReducer } from "react";
  2. import { PorterFormData, PorterFormState, PorterFormAction } from "./types";
  3. interface Props {
  4. formData: PorterFormData;
  5. }
  6. interface ContextProps {
  7. formData: PorterFormData;
  8. dispatchAction: (event: PorterFormAction) => void;
  9. }
  10. export const PorterFormContext = createContext<ContextProps | undefined>(
  11. undefined!
  12. );
  13. const { Provider } = PorterFormContext;
  14. export const PorterFormContextProvider: React.FC<Props> = (props) => {
  15. const [state, dispatch] = useReducer(
  16. (state: PorterFormState, action: PorterFormAction) => {
  17. console.log(action);
  18. return state;
  19. },
  20. {
  21. components: [],
  22. }
  23. );
  24. return (
  25. <Provider value={{ formData: props.formData, dispatchAction: dispatch }}>
  26. {props.children}
  27. </Provider>
  28. );
  29. };