I will try to explain my problem, I have created a custom Context API wrapper for all my data. Now, I have this docType which is not always defined nor existing.
When I destructure it like so:
const { docType } children?.props?.data; // My Next App / JS crashes and getting the undefined error.
Doing this works:
const docType = children.props.data?.docType;
I am not really sure why this happens. destructuring all other data props will fail if I don't write my code like so:
import { createContext, useContext } from "react";
const AppContext = createContext();
export const AppWrapper = ({ children }) => {
const docType = children.props.data?.docType;
const site = children.props.data?.site;
const page = children.props.data?.page;
const preview = children.props?.preview;
const sharedState = {
docType,
preview,
page,
site,
};
return (
<AppContext.Provider value={sharedState}>{children}</AppContext.Provider>
);
};
export const useAppContext = () => useContext(AppContext);
Can someone explain to me why I am getting this problem?
Via Active questions tagged javascript - Stack Overflow https://ift.tt/l6dq5pC
Comments
Post a Comment