這可能是一個更普遍的 React 問題,但是我如何包裝像TextFieldmui這樣的組件并提供新的 props 舊的 props?到目前為止我已經嘗試過這個,但我不明白如何訪問其他道具。
import React from 'react';
import { TextField, TextFieldProps } from '@material-ui/core';
interface MyArgs {
customProp?: boolean;
}
export type MyProps = MyArgs & TextFieldProps;
export const MyCustomInput: React.FC<MyProps> = ({
customProp = false
}) => {
return (
<TextField
// How to access things from TextFieldProps?
required={??}
/>
)
}
uj5u.com熱心網友回復:
您可以TextFieldProps從組件介面擴展:
import { TextField, TextFieldProps } from '@material-ui/core';
interface MyCustomInputProps extends TextFieldProps {
customProp?: boolean;
}
export const MyCustomInput = (props: MyCustomInputProps) => {
const { customProp = false, ...textFieldProps } = props;
// Do whatever you want with `customProp`
return (
<TextField {...textFieldProps} />
);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/399851.html
上一篇:在TSX上沒有過載匹配此呼叫錯誤
