ListItem(材质 UI 组件)中的 React Typescript prop 错误

时间:2021-03-28 16:21:11

标签: reactjs typescript types material-ui react-props

我遇到了 ContainerComponent 和 ContainerProps 的问题,我认为问题在于“li”是字符串类型,我需要明确地将其定义为 React.ElementType,但我不知道如何。

<ListItem
  ContainerComponent="li"
  ContainerProps={{ ref: provided.innerRef }}
  {...provided.draggableProps}
  {...provided.dragHandleProps}
  style={getItemStyle(
      snapshot.isDragging,    
      provided.draggableProps.style
  )}
>

我遇到的错误:

没有与此调用匹配的过载。 最后一次重载给出了以下错误。 类型 '"li"' 不能分配给类型 'ElementType |不明确的'。 输入 '{ ref: (element?: HTMLElement | null | undefined) => any; }' 不可分配给类型 'HTMLAttributes'。 对象字面量只能指定已知属性,并且类型 'HTMLAttributes' 中不存在 'ref'。ts(2769)

1 个答案:

答案 0 :(得分:0)

通过写作解决了这个问题:

<ListItem
ContainerComponent={(<li />).type}
ref={provided.innerRef}                                             
{...provided.draggableProps}                                                 
{...provided.dragHandleProps}
style={getItemStyle                                                      
snapshot.isDragging,                                                      
provided.draggableProps.style)}