I have a HeaderAvatarMenu
component which is a dropdown menu, and it has a state of toggle.
const ToggleContext = createContext();
const HeaderAvatarMenu = ({ children, ...restProps }) => {
const [toggle, setToggle] = useState(false);
return (
<ToggleContext.Provider value={{ toggle, setToggle }}>
<Container {...restProps}>{children}</Container>
</ToggleContext.Provider>
);
};
For this component I have created a compound component Link
which consumes the context values
HeaderAvatarMenu.Link = function ({ children, ...restProps }) {
const { toggle, setToggle } = useContext(ToggleContext);
return (
<ReactLink onClick={() => setToggle((toggle) => !toggle)} {...restProps}>
{children}
</ReactLink>
);
};
Error :
React Hook "useContext" is called in function "HeaderAvatarMenu.Link" that is neither a React function component nor a custom React Hook function. React component names must start with an uppercase letter
package.json
"react": "^17.0.1",
"react-bootstrap": "^1.4.0",
"react-dom": "^17.0.1",
"react-icons": "^3.11.0",
"react-router-dom": "^5.2.0",
What could be the possible solution to use context within compound component?