So I found the 5-6 other questions I actually kinda fixed it with " ! " operator, so don't delete the question for duplication, but what I don't understand is the variable is not an object. So this is the context;
import { createContext } from "react";
interface PlanetInfos {
planetName: string;
radius: string;
distanceFromSun?: string;
numberOfMoons?: string;
averageTemperature: string;
svg: string;
}
type ContextProps = {
miliSeconds: number;
setmiliSeconds: any;
planetInfos: PlanetInfos[];
};
const PlanetContext = createContext<Partial<ContextProps>>({});
export default PlanetContext;
here is the App.js where I define the miliseconds for example;
const App: React.FC = () => {
const [miliSeconds, setmiliSeconds] = useState(10000);
return (
<PlanetContext.Provider
value={{
miliSeconds,
setmiliSeconds,
planetInfos,
}}
>
<div className="App">
<SideBar />
<Planets />
</div>
</PlanetContext.Provider>
);
};
export default App;
So in wherever I do this and use miliseconds somewhere in the component;
const { miliSeconds } = useContext(PlanetContext);
It says "Object is possibly undefined" and it doesn't make sense for me. Because it was never an object. So is there any solution besides putting exclamation mark everywhere in the app?
createContext<ContextProps>({} as ContextProps) ;or perhapscreateContext({} as ContextProps) ;setmiliSeconds: (ms: number) => void