i want to fix the error "cannot invoke an object which can possibly be undefined" using react and typescript.
what i am trying to do? I am using usecontext react hook and creating a variable from it (dialogContext) in the components (home, Dialog and books component). In doing so i get the above mentioned error.
I am defining DialogContext in helper file like below
interface ContextProps {
setDialogOpen?: (open: boolean) => void;
}
export const DialogContext = React.createContext<ContextProps>({});
And importing DialogContext in components where needed that is (Main, home, Dialog components)
function MainComponent() {
let [showDialog, setShowDialog] = React.useState(false);
return (
<DialogContext.Provider
value={{
setDialogOpen: (open: boolean) => {
if (open) {
const sessionDialogClosed = sessionStorage.getItem('dialog');
if (sessionDialogClosed !== 'closed') {
setShowDialog(open);
sessionStorage.setItem('dialog', 'closed');
}
} else {
setShowDialog(open);
}
},
}}
>
{showDialog && <Dialog DialogContext={DialogContext}/>
<Route
path="/items">
<Home />
</Route>
<Route
path="/id/item_id">
<Books/>
</Route>
</DialogContext.Provider>
)
}
function Home() {
const dialogContext= React.useContext(DialogContext);
const handleClick = () {
dialogContext.setDialogOpen(true); //get error here
}
return (
<button onClick={handleClick}>add</button>
)
}
function Books({DialogContext} : Props) {
const dialogContext= React.useContext(DialogContext);
const handleClick = () {
dialogContext.setDialogOpen(true); //get error here
}
return (
<button onClick={handleClick()}>Click me</button>
)
}
function Dialog() {
return(
<div>
//sometext
<button onClick={dialogContext.setDialogOpen(false)}> hide</button> //get errror here
</div>
)
}
What i have tried?
I have added a check for undefined where dialogContext is used in components (Books, Home, Dialog) like for example in Books components i used like below,
function Books({DialogContext} : Props) {
const dialogContext= React.useContext(DialogContext);
const handleClick = () {
if (dialogContext !== 'undefined') {
dialogContext.setDialogOpen(true); //get error here
}
}
return (
<button onClick={handleClick()}>Click me</button>
)
}
But still the error "cannot invoke an object which can possibly be undefined" throws.
Could someone help me fix this error. thanks.
Edit:
I have tried to do below and it removes the error
function Books({DialogContext} : Props) {
const dialogContext= React.useContext(DialogContext);
const handleClick = () {
if (dialogContext && dialogContext.setDialogOpen) {
dialogContext.setDialogOpen(true);
}
}
return (
<button onClick={handleClick()}>Click me</button>
)
}
But instead of adding a check like this in every component what change should i make in the DialogContext helper file or what needs to be changed to fix keep checking for undefined or not. thanks.
dialogContext?.setDialogOpenwork?