【发布时间】:2021-03-12 08:35:42
【问题描述】:
如果在 Array.map 中返回 MenuItem,当我按下 Select 时会出错。
代码
const MenuItems: React.FC<{ items: number[] }> = (props) => {
const { items } = props;
return (
<>
{items.map((i) => {
return (
<MenuItem key={i} value={i}>
{i}
</MenuItem>
);
})}
</>
);
};
错误
Warning: Function components cannot be given refs. Attempts to access this ref will fail.
Did you mean to use React.forwardRef()?
所以想改写成forwardRef,但是不知道ref的类型。是否可以首先对 MenuItem 使用 forwardRef ?
谢谢。
【问题讨论】:
-
从您向我们展示的代码中很难判断您的问题是什么。里面没有使用 refs,也许你可以告诉我们你试图在哪里传递 refs 或者你的 items 道具上有什么?
-
我想避免深度嵌套,但我收到一条消息,提示需要 forwardRef。我不知道该怎么做。
标签: reactjs typescript material-ui