How to pass Parent props boolean in to child component when using dot notation with functional component.
Component structure is
<Dropdown className="float-right">
<Dropdown.Title>Open menu</Dropdown.Title>
<Dropdown.Menu align="end">
<Dropdown.Item className="hello mate" href="/dashboard">
Dashboard
</Dropdown.Item>
<Dropdown.Item className="hello mate" href="/settings">
Settings
</Dropdown.Item>
<Dropdown.Divider />
<Dropdown.Item className="hello mate" onClick={signOut}>
Sign out
</Dropdown.Item>
</Dropdown.Menu>
</Dropdown>
Child component <Dropdown.Title> has OnClick to open `<Dropdown.Menu>
const DropdownTitle = (props) => {
const { children, className } = props;
const [openDropdown, setOpenDropdown] = useState(false);
const toggleDropdown = () => setOpenDropdown(!openDropdown);
return (
<button type="button" onClick={toggleDropdown}>{children}</button>
);
};
How to pass the openDropdown prop to <Dropdown.Menu> from <Dropdown.Title>
Looking for solution with managing with openDropdown.tsx which can avoid additional coding when using the component <Dropdown>
Modified code sandbox
What you can do is create a context that wraps your parent
Dropdowncomponent usingReact.createContext. Then all the children of theDropdowncan receive the values that are passed through the context provider.Set the
valueprop of theProvidercomponent to be the state of the dropdown, and the dropdown toggle function.Then, to consume your context values, use React's
useContexthook in the components that need them.This solution will allow you to keep all the values incapsulated into one file. Similar to the bootstrap method you linked here: https://react-bootstrap.netlify.app/docs/components/dropdowns/