context.tsx 650 B

123456789101112131415161718192021222324252627
  1. import {
  2. createContext,
  3. useRef,
  4. } from 'react'
  5. import type {
  6. FeaturesState,
  7. FeaturesStore,
  8. } from './store'
  9. import { createFeaturesStore } from './store'
  10. export const FeaturesContext = createContext<FeaturesStore | null>(null)
  11. type FeaturesProviderProps = {
  12. children: React.ReactNode
  13. } & Partial<FeaturesState>
  14. export const FeaturesProvider = ({ children, ...props }: FeaturesProviderProps) => {
  15. const storeRef = useRef<FeaturesStore>()
  16. if (!storeRef.current)
  17. storeRef.current = createFeaturesStore(props)
  18. return (
  19. <FeaturesContext.Provider value={storeRef.current}>
  20. {children}
  21. </FeaturesContext.Provider>
  22. )
  23. }