context.tsx 612 B

123456789101112131415161718192021222324
  1. import {
  2. createContext,
  3. useRef,
  4. } from 'react'
  5. import { createWorkflowStore } from './store'
  6. type WorkflowStore = ReturnType<typeof createWorkflowStore>
  7. export const WorkflowContext = createContext<WorkflowStore | null>(null)
  8. type WorkflowProviderProps = {
  9. children: React.ReactNode
  10. }
  11. export const WorkflowContextProvider = ({ children }: WorkflowProviderProps) => {
  12. const storeRef = useRef<WorkflowStore>()
  13. if (!storeRef.current)
  14. storeRef.current = createWorkflowStore()
  15. return (
  16. <WorkflowContext.Provider value={storeRef.current}>
  17. {children}
  18. </WorkflowContext.Provider>
  19. )
  20. }