简体   繁体   English

React Material UI Autocmplete,React-window 在列表顶部重新渲染

[英]React Material UI Autocmplete with React-window rerenders at top of list

I have recently started learning React and could use any help.我最近开始学习 React 并且可以使用任何帮助。

I am having an issue with the Material-ui autocomplete component.我遇到了 Material-ui 自动完成组件的问题。 Need some real React gurus to figure this one out.需要一些真正的 React 大师来解决这个问题。 The problem I am having is that when I select an option from the autocomplete drop down it resets to the top of the list.我遇到的问题是,当我 select 自动完成下拉菜单中的一个选项时,它会重置到列表的顶部。 I am using react-window because the list is over 300 elements.我正在使用 react-window,因为列表中有 300 多个元素。 The ingredient filter is the one that I am trying to fix, the other filters work fine.成分过滤器是我正在尝试修复的过滤器,其他过滤器工作正常。 I have created code sandboxes that demonstrate what should happen and what is currently happening.我创建了代码沙箱来演示应该发生什么以及当前正在发生什么。 I have messed around with React.memo but that does not have any affect on it.我已经搞砸了 React.memo 但这对它没有任何影响。 Please let me know if you have any ideas.如果您有任何想法,请告诉我。 The correct sandbox is from the material-ui site: autcomplete component正确的沙箱来自 material-ui 站点: autcomplete 组件

Correct: correct sandbox正确:正确的沙箱

correct gif正确的gif

Issue: issue sandbox问题:问题沙箱

issue gif问题 gif

Please let me know if you need any clarification如果您需要任何澄清,请告诉我

I had to use useCallback on ListboxComponent, please try now.我必须在 ListboxComponent 上使用 useCallback,请立即尝试。 Link to code: https://codesandbox.io/s/material-demo-33l5y?file=/demo.js:11315-11331代码链接: https://codesandbox.io/s/material-demo-33l5y?file=/demo.js:11315-11331

Old answer:老答案:

Seems like when we call setIngredients it is re-rendering the component and causes the list to jump to top, memoizing the handleIngredients function seems to fix it.似乎当我们调用 setIngredients 时,它正在重新渲染组件并导致列表跳到顶部,记住句柄 Ingredients function 似乎可以解决它。

const handleIngredients = React.useMemo((event, value) => {
  setIngredients(value);
}, []);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM