Files
spestexnika/components/lib_components/carDetailProvider.tsx
nabijonovdavronbek619@gmail.com 051d9053dc carType page over
2025-11-07 20:00:27 +05:00

34 lines
1011 B
TypeScript
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use client';
import React, { createContext, useContext, useState, ReactNode } from 'react';
import { innerCardTypes } from '@/types';
// 1⃣ Context tipi
interface CarDetailContextType {
detail: innerCardTypes | null;
setDetail: React.Dispatch<React.SetStateAction<innerCardTypes | null>>;
}
// 2⃣ Default context qiymatini yaratamiz
const CarDetailContext = createContext<CarDetailContextType | undefined>(undefined);
// 3⃣ Provider komponent
export const CarDetailProvider = ({ children }: { children: ReactNode }) => {
const [detail, setDetail] = useState<innerCardTypes | null>(null);
return (
<CarDetailContext.Provider value={{ detail, setDetail }}>
{children}
</CarDetailContext.Provider>
);
};
// 4⃣ Custom hook (Contextni qulay ishlatish uchun)
export const useCarDetail = () => {
const context = useContext(CarDetailContext);
if (!context) {
throw new Error('useCarDetail must be used within a CarDetailProvider');
}
return context;
};