Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReactJS hooks: how to useContext in two different .js files?

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?

like image 997
devamat Avatar asked Sep 09 '19 08:09

devamat


People also ask

What is the usecontext hook in react?

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.

How to import appcontext inside of a React component?

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:

What is react context in React React?

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.

How to create a C ontext object in react?

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.


1 Answers

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>
  );
}
like image 153
Dennis Vash Avatar answered Oct 13 '22 01:10

Dennis Vash