I have two components in the same .js file using React.createContext() and React.useContext(). Now I would like to move the Menu component in another .js file but I'm having trouble using the same context. This is the code:
const ManagerContext = React.createContext(null);
export default function LessonManager() {
const [title, setTitle] = React.useState('SomeOtherTitle');
const [editing, toggleEditor] = React.useState(false);
const value = React.useMemo(() => {
return {
title,
setTitle,
editing,
toggleEditor,
log: (t) => console.log(t)
}
}, [title, editing]);
return (
<ManagerContext.Provider value={value}>
<div className='box-default expand'>
<div className='handle' style={{display: 'flex', justifyContent: 'center', width: '100%', cursor: 'grab'}}>
<LessonMenu/>
</div>
</div>
</ManagerContext.Provider>
)
}
export function LessonMenu() {
const state = React.useContext(ManagerContext);
return (
<ButtonGroup size='sm'>
<IconButton
className='rsuite-btn menu-button'
onClick={()=>state.toggleEditor(!state.editing)}
icon={ <Icon icon={state.editing ? ('eye') : ('edit2')} />} />
</ButtonGroup>
)
}
I tried to export the const ManagerContext:
export const ManagerContext = React.createContext(null);
and import it in the Menu.js file I created with the Menu component, but it doesn't work. What am I doing wrong?
I recommend skimming through the official React documentation on Context before continuing. How Does the useContext Hook Work? The useContext Hook provides all the same functionality you’d expect from the Context API, just packaged up into a simple to use Hook that you can use inside functional components.
import AppContext from './appContext.js'; class Example extends React.Component { static context = AppContext; render() { let value = this. context; ... } } And below is the same Context object inside of a functional component, using the new useContext Hook:
React Context is a way to manage state globally. It can be used together with the useState Hook to share state between deeply nested components more easily than with useState alone. State should be held by the highest parent component in the stack that requires access to the state.
You create a C ontext object in React by using React.CreateContext, and then passing in an initial value, like so: const AppContext = React . createContext ( { foo : 'bar' } ) ; This AppContext object is what should be passed as an argument into the useContext Hook.
Your code seems to work, note how you need to import the context:
// export const ManagerContext = React.createContext(null);
import { ManagerContext } from "./Menu.js";
export function LessonMenu() {
const { toggleEditor, editing } = React.useContext(ManagerContext);
return (
<ButtonGroup size="sm">
<IconButton
className="rsuite-btn menu-button"
onClick={() => toggleEditor(!editing)}
icon={<Icon icon={toggleEditor ? "eye" : "edit2"} />}
/>
</ButtonGroup>
);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With