【发布时间】:2021-10-07 12:22:01
【问题描述】:
我需要将参数category 传递给CustomTreeItem,即TreeItemContent。
文档:https://mui.com/ru/api/tree-item/
import TreeItem, {
TreeItemProps,
useTreeItem,
TreeItemContentProps,
} from '@material-ui/lab/TreeItem';
interface CatProps {
name: string,
id: string,
}
interface CatItemContentProps extends TreeItemContentProps {
category: CatProps,
}
const CustomContent = React.forwardRef(function CustomContent(
props: CatItemContentProps,
ref,
) {
const { category } = props;
console.log(category); // undefined
return <></>;
}
);
interface CatItemProps extends TreeItemProps {
category: CatProps,
}
const CustomTreeItem = (props: CatItemProps) => {
return (
<TreeItem ContentComponent={CustomContent} {...props} />
);
};
我该怎么做?
第一次回答后的澄清
新代码是:
const CustomContent = React.forwardRef(function CustomContent(
props: TreeItemContentProps,
ref
) {
return (
<div ref={ref as React.Ref<HTMLDivElement>}>
{props.label} {props.myProps}
</div>
);
});
const CustomTreeItem = (props: TreeItemProps) => {
return <TreeItem ContentComponent={CustomContent} {...props} />;
};
export default function App() {
return (
<TreeView>
<CustomTreeItem
nodeId="1"
label="Applications"
ContentProps={{
myProps: "my props"
}}
></CustomTreeItem>
</TreeView>
);
}
我已经在代码沙箱中制作了它,它工作正常:
https://codesandbox.io/s/clever-volhard-km9xf?file=/src/App.tsx
但我的代码有一个新错误:
Type '{ myProps: string; }' is not assignable to type 'HTMLAttributes<HTMLElement>'.
Object literal may only specify known properties, and 'myProps' does not exist in type 'HTMLAttributes<HTMLElement>'
Google 从另一个人那里给了我同样的问题:
https://issueexplorer.com/issue/mui-org/material-ui/28668
如何将我的自定义道具传递给 ContentProps?
【问题讨论】:
-
您编辑问题时我没有收到通知,如果您想提出后续问题,可以在我的回答中发表评论,这样您的消息就会显示在我的收件箱中。
标签: reactjs typescript material-ui