[REECT] [MUI]警告:在渲染其他组件时无法更新组件
我正在使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要从
handlefilteroptions
移出setState
函数。通过
setState
内部oninputChange
prop。都是因为渲染行为
自动完成
组件。You need to move out
setState
function fromhandleFilterOptions
.Pass
setState
insideonInputChange
prop.All because of render behavior
Autocomplete
component.