最好的选择是创建一个简单的NoTransition 组件:
export const NoTransition = React.forwardRef<
React.ReactFragment,
TransitionProps
// eslint-disable-next-line @typescript-eslint/no-unused-vars
>(({ children }, ref) => {
return <>{ children }</>;
});
然后做TransitionComponent={ NoTransition }。
另外,不要省略ref 参数,否则你也会收到警告:
警告:forwardRef 渲染函数只接受两个参数:props 和 ref。忘记使用 ref 参数了吗?
只添加TransitionProps={{ timeout: 0 }},不设置TransitionComponent也可以,但在这种情况下不需要渲染默认的TransitionComponent。
其他答案中提出的一些选项会引发错误或警告:
执行TransitionComponent={ Fragment } 将导致以下警告:
警告:提供给 React.Fragment 的无效属性 appear。 React.Fragment 只能有 key 和 children 属性。
执行TransitionComponent={ ({ children }) => children } 将导致其他警告(可能会根据您使用Tooltip、Modal 或其他组件而有所改变):
警告:失败的道具类型:无效的道具children 提供给ForwardRef(Modal)。期望一个可以容纳 ref 的元素。您是否不小心将普通函数组件用于元素?欲了解更多信息,请参阅https://mui.com/r/caveat-with-refs-guide
警告:道具类型失败:提供给 ForwardRef(ModalUnstyled) 的道具 children 无效。期望一个可以容纳 ref 的元素。您是否不小心将普通函数组件用于元素?欲了解更多信息,请参阅https://mui.com/r/caveat-with-refs-guide
警告:函数组件不能被赋予 refs。尝试访问此 ref 将失败。你的意思是使用 React.forwardRef() 吗?检查Unstable_TrapFocus的渲染方法。
还有可能出现这个错误:
未捕获的错误:超出最大更新深度。当组件在 componentWillUpdate 或 componentDidUpdate 中重复调用 setState 时,可能会发生这种情况。 React 限制了嵌套更新的数量以防止无限循环。
我正在使用"@mui/material": "^5.5.2"。