[Doc] Multiple states within a map
See original GitHub issueNice library! I am currently struggling to find the most optimal way to use the hooks
within a map to keep track of multiple menus’ state.
This is the dirty code - really not optimal - I came up with so far:
export default () => {
const resourcesMenuState = usePopupState({
popupId: "resources",
variant: "popover",
});
const servicesMenuState = usePopupState({
popupId: "services",
variant: "popover",
});
return (
<nav>
<MenuList disablePadding className={classes.menuList}>
{routes.map((route) => (
<div key={uid(route)}>
<MenuItem
{...(route.name === "Resources" && bindHover(resourcesMenuState))}
{...(route.name === "Services" && bindHover(servicesMenuState))}
// --- SNIP ---
>
{route.name}
</MenuItem>
{route.routes && (
<Menu
{...(route.name === "Resources" &&
bindMenu(resourcesMenuState))}
{...(route.name === "Services" && bindMenu(servicesMenuState))}
// --- SNIP ---
>
{route.routes.map((nestedRoute) => (
<MenuItem
key={uid(nestedRoute)}
// Dirty hack - can handle only 2 menus
onClick={
route.name === 'Resources'
? resourcesState.close
: servicesState.close
}
// --- SNIP ---
>
{nestedRoute.name}
</MenuItem>
))}
</Menu>
)}
</div>
))}
</MenuList>
</nav>
);
};
Could you please provide an optimal example on how to achieve this?
Issue Analytics
- State:
- Created 3 years ago
- Comments:12 (6 by maintainers)
Top Results From Across the Web
Map - AWS Step Functions
Map states can use a variety of input types, including a JSON array, a list of Amazon S3 objects, or a CSV file....
Read more >Display Multiple State Values in Map Visualization
Display Multiple State Values in Map Visualization ; 1, Alabama; Alaska ; 2, Alabama ; 3, Alabama; Arkansas ; 4, California; New York;...
Read more >How to Update State in map function in react - Stack Overflow
Updating the state inside the map will cause multiple renderings(as the state is updating) and it's not good.
Read more >Adding a Map and Markers to a React Application
Adding a Map and Markers to a React Application · Overview · Install @googlemaps/react-wrapper · Add a map component · Extend map component...
Read more >DAIFacilitiesMap.pdf - WI DOC
Fond du Lac. Rock. Winnebago. Iron. Rusk. Oneida. Door. Sawyer. Grant. Wood. Brown. Clark. Oconto. Green. Racine. Pepin. Waupaca. Buffalo. Iowa. Dane.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@aress31 just got back from a vacation. Sorry for forgetting about this ticket. Here’s how I would recommend solving it:
Here are the reasons I’m not in favor of a setter for
popupId
or being able to use a singleusePopupState
for multiple popup menus:popupId
to workbindTrigger(popupState)
returns an object withonClick: popupState.open
, wherepopupState.open
is a memoized function. If I made abindTrigger(popupState, { popupId: 'foo' })
API, then it would have to return something likeonClick: () => popupState.open({ popupId: 'foo' })
, causing theMenuItem
to rerender each time becauseonClick
is always a new function instance.@jedwards1211 too advanced for me, I am just a hobbyist thanks for the warning though but yeah as you said number of routes is and will remain constant so in my case I guess it is ok…