Reputation: 21
I am new to react JS and stuck in a very basic issue. I am using Bootstrap 'Offcanvas' component but the problem is that the button to toggle it is header while the menu itself is in different file. I dont know how to define global const to manage it
CONST
const [showSidebar, setShowSidebar] = useState(false);
const handleSidebarClose = () => setShowSidebar(false);
const handleSidebarShow = () => setShowSidebar(true);
HEADER.JS
<Button variant="primary" onClick={handleSidebarShow}>Show Meny</Button>
SIDEBAR.JS
<Offcanvas show={showSidebar} onHide={handleSidebarClose}>
<Offcanvas.Body>
Some text as placeholder. In real life you can have the elements you
have chosen. Like, text, images, lists, etc.
</Offcanvas.Body>
</Offcanvas>
Where should i place these consts and make them global?
Upvotes: 0
Views: 107
Reputation: 2250
You have to put the state, that is needed by several other components above them. This is called "lifting the state". Then hand over the state or/and your functions to the components below via props. For example:
Please look into following sandbox for a working example: https://codesandbox.io/s/vigilant-mopsa-5f6us9
Upvotes: 1