question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] Items map does not reflect current state of ScrollMenu children

See original GitHub issue

Describe the bug In my project, we have a use case to add & remove items from a ScrollMenu. When adding to the end of the collection, the ScrollMenu will correctly render the new item, but the items and isLastItemVisible api for the ScrollMenu seem to be behind, causing the RightArrow component not to be rendered, even though it should. We are also using a hook on the new apiRef to detect changes in the size of apiRef.current.items.size to automatically scroll to the end of the collection on adding a new item, and this also does not seem to work because the items map has not correctly been updated.

Additional Context To start, we have 5 placeholder items in the ScrollMenu, and those get removed as we add items to the actual collection. As I mentioned, we also have functionality that will add and remove from the items collection.

Here is an example of the code for reference:

const scrollMenuApiRef = useRef({});
const prevMenuItems = usePrevious(scrollMenuApiRef?.current?.items?.size);

const placeholderItems = [];

if (items.length < 5) {
  for (let i = 0; i < 5 - items.length; i++) {
    placeholderItems.push(
      <PlaceholderItem
        key={`placeholder-${i}`}
        itemId={`carousel-list-${items.length - i}`}
      />
    );
  }
}

useEffect(() => {
  if (scrollMenuApiRef.current.items.size > prevMenuItems) {
    scrollMenuApiRef.current.scrollToItem(
      scrollMenuApiRef.current.getItemById(items[items.length - 1].id), 'smooth', 'end', 'nearest'
    );
  }
}, [scrollMenuApiRef?.current?.items?.size]);

return (
  <ScrollMenu
    LeftArrow={LeftArrow}
    RightArrow={RightArrow}
    wrapperClassName={classes.wrapper}
    apiRef={scrollMenuApiRef}
  >
    {items.map((item) => (
      <Item
        key={`carousel-list-${item.id}`}
        itemId={item.id}
      />
    ))}
    {placeholderItems}
  </ScrollMenu>
);

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
asmyshlyaev177commented, Oct 16, 2021

There is no way to update apiRef before useEffect, but i figure this out. Can just get last item directly via data-key attribute.

@asmyshlyaev177, this solution seems to work well! Thank you for your help here. Would it be worth adding an scrollToItemByItemId to the public api? It seems like a common scenario to know an item id from outside of the context of the ScrollMenu, and the scrollToItem(getItemById(...)) combination seems less reliable without access to the most recent ScrollMenu.items.

Ok, added getItemElementById and getItemElementByIndex helpers.

0reactions
jameslee523commented, Oct 15, 2021

There is no way to update apiRef before useEffect, but i figure this out. Can just get last item directly via data-key attribute.

@asmyshlyaev177, this solution seems to work well! Thank you for your help here. Would it be worth adding an scrollToItemByItemId to the public api? It seems like a common scenario to know an item id from outside of the context of the ScrollMenu, and the scrollToItem(getItemById(...)) combination seems less reliable without access to the most recent ScrollMenu.items.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable to map objects into React horizontal Menu
I want to map title of contents of first sections for example. I have provided the data as well the code of the...
Read more >
Kalium Changelog - Documentation Laborator Themes
Kalium is constantly being updated with new features, bug fixes, and adjustments. We recommend you to read this changelog for every update ...
Read more >
Bug: Scroll wheel does not work in IE 11 [35823692]
Bug : Scroll wheel does not work in IE 11 ... https://google-developers.appspot.com/maps/documentation/javascript/examples/full/map-simple.
Read more >
BCGControlBar Pro for MFC: versions - BCGSoft
When this parameter is TRUE, the menu is not converted to sub-items list. ... The recent QAT state is automatically saved in the...
Read more >
Jeffrey's “Geoencoding Support” Plugin for Lightroom
Updated the "View at Bing" stuff to work with Bing's current maps. ... set via pulldown in the geoencoding dialog) does not represent...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found