我正在尝试使用我编写的这个组件:
import React from 'react';
import MenuItem from '@material-ui/core/MenuItem';
import ListItemText from '@material-ui/core/ListItemText';
const ITEMS = {
item1: { id: 1, name: '1', description: 'item1', protected: true },
item2: { id: 2, name: '2', description: 'item2' },
item3: { id: 3, name: '3', description: 'item3' },
}
// See https://github.com/DefinitelyTyped/DefinitelyTyped/issues/20356#issuecomment-435708501
// for an explanation on why the component return type was cast to `any` below.
const MenuItemList: any = () => {
return Object.values(ITEMS).map(
(item) =>
!item.protected && (
<MenuItem key={item.id} value={item.id}>
<ListItemText
primary={item.name}
secondary={item.description}
/>
</MenuItem>
)
);
};
export default MenuItemList;
...一次在Textfield
类型内select
,另一次在Menu
组件内。但是,在浏览器中访问它时出现以下错误:
Warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?
关于如何解决这个问题的任何想法?
更新:
这是我如何调用这个组件:
<Menu
id={id}
open={open}
anchorEl={anchorEl}
getContentAnchorEl={null}
keepMounted={false}
onClose={handleClose}
elevation={2}
PaperProps={{
square: true,
}}
anchorOrigin={{
vertical: 'bottom',
horizontal: 'right',
}}
transformOrigin={{
vertical: 'top',
horizontal: 'right',
}}
>
< MenuItemList />
</Menu>
<Field
name="items"
label="Select Item"
padding={2}
component={TextField}
select
fullWidth
SelectProps={{
MenuProps: {
elevation: 2,
getContentAnchorEl: null,
anchorOrigin: {
vertical: 'bottom',
horizontal: 'left',
},
transformOrigin: {
vertical: 'top',
horizontal: 'left',
},
},
IconComponent: ExpandMoreIcon,
}}
variant="filled"
InputProps={{
disableUnderline: true,
}}
>
<MenuItemList />
</Field>
更新 2
这是错误堆栈:
Check the render method of `ForwardRef(Menu)`.
in MenuItemList (at ShowItemsDialog.tsx:105)
in ul (created by ForwardRef(List))
in ForwardRef(List) (created by WithStyles(ForwardRef(List)))
in WithStyles(ForwardRef(List)) (created by ForwardRef(MenuList))
in ForwardRef(MenuList) (created by ForwardRef(Menu))
in div (created by ForwardRef(Paper))
in ForwardRef(Paper) (created by WithStyles(ForwardRef(Paper)))
in WithStyles(ForwardRef(Paper)) (created by Transition)
in Transition (created by ForwardRef(Grow))
in ForwardRef(Grow) (created by TrapFocus)
in TrapFocus (created by ForwardRef(Modal))
in div (created by ForwardRef(Modal))
in ForwardRef(Portal) (created by ForwardRef(Modal))
in ForwardRef(Modal) (created by ForwardRef(Popover))
in ForwardRef(Popover) (created by WithStyles(ForwardRef(Popover)))
in WithStyles(ForwardRef(Popover)) (created by ForwardRef(Menu))
in ForwardRef(Menu) (created by WithStyles(ForwardRef(Menu)))
in WithStyles(ForwardRef(Menu)) (created by ForwardRef(SelectInput))
in ForwardRef(SelectInput) (created by ForwardRef(InputBase))
in div (created by ForwardRef(InputBase))
in ForwardRef(InputBase) (created by WithStyles(ForwardRef(InputBase)))
in WithStyles(ForwardRef(InputBase)) (created by ForwardRef(FilledInput))
in ForwardRef(FilledInput) (created by WithStyles(ForwardRef(FilledInput)))
in WithStyles(ForwardRef(FilledInput)) (created by ForwardRef(Select))
in ForwardRef(Select) (created by WithStyles(ForwardRef(Select)))
in WithStyles(ForwardRef(Select)) (created by ForwardRef(TextField))
in div (created by ForwardRef(FormControl))
in ForwardRef(FormControl) (created by WithStyles(ForwardRef(FormControl)))
in WithStyles(ForwardRef(FormControl)) (created by ForwardRef(TextField))
in ForwardRef(TextField) (created by WithStyles(ForwardRef(TextField)))
in WithStyles(ForwardRef(TextField)) (created by FormikMaterialUITextField)
in FormikMaterialUITextField (created by Field)
in Field (at ShowItemsDialog.tsx:77)