[REECT] [MUI]警告:在渲染其他组件时无法更新组件

发布于 2025-02-08 07:40:33 字数 1007 浏览 2 评论 0原文

我正在使用AutoComplete(MUI)来创建一个国家选择器,但是下面收到了错误消息。

Warning: Cannot update a component (`CountrySelectorPopup`) while rendering a different component (`ForwardRef(Autocomplete)`). To locate the bad setState() call inside `ForwardRef(Autocomplete)`

只有当我有一个setState setState(过滤[Filled.length -1])内部的内部,此错误才会发生。

const handleFilterOptions = useCallback((
    options: TermObject[],
    params: FilterOptionsState<TermObject>
  ) => {
    const filtered = filter(options, params);
    setState(filtered[filtered.length - 1]);
    return filtered;
  }, [1]);

然后我将此功能传递给自动完成过滤器。

<Autocomplete
  clearOnBlur
  fullWidth
  freeSolo
  onChange={(_e, _newValue) => setValue("")}
  onInputChange={(_e, newValue) => setInputValue(newValue)}
  value={value}
  inputValue={inputValue}
  open={true}
  filterOptions={handleFilterOptions}...

我相信这是自动完成的相关错误,但是任何人都可以告诉我为什么会发生这种情况,因为这似乎不是对我的糟糕的呼叫。

I am using AutoComplete(MUI) to create a country selector, but I got the error message below.

Warning: Cannot update a component (`CountrySelectorPopup`) while rendering a different component (`ForwardRef(Autocomplete)`). To locate the bad setState() call inside `ForwardRef(Autocomplete)`

This error happends only when I have a setState setState(filtered[filtered.length - 1]) inside of the function.

const handleFilterOptions = useCallback((
    options: TermObject[],
    params: FilterOptionsState<TermObject>
  ) => {
    const filtered = filter(options, params);
    setState(filtered[filtered.length - 1]);
    return filtered;
  }, [1]);

And I pass this function to AutoComplete filterOptions.

<Autocomplete
  clearOnBlur
  fullWidth
  freeSolo
  onChange={(_e, _newValue) => setValue("")}
  onInputChange={(_e, newValue) => setInputValue(newValue)}
  value={value}
  inputValue={inputValue}
  open={true}
  filterOptions={handleFilterOptions}...

I believe this is AutoComplete related error, but could anyone tell me why this happens because it doesn't seems like a bad setState call to me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泪痕残 2025-02-15 07:40:34

您需要从handlefilteroptions移出setState函数。
通过setState内部oninputChange prop。

都是因为渲染行为自动完成组件。

You need to move out setState function from handleFilterOptions.
Pass setState inside onInputChange prop.

All because of render behavior Autocomplete component.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文