我正在嘗試創建一個渲染道具包裝器組件。它正在作業,但我收到型別錯誤。這也是我指向代碼沙箱的鏈接。沙盒鏈接
- 我得到的第一個錯誤是 children 是一個不可呼叫的運算式。
- 第二個錯誤是“你好”隱含任何型別。
尋找 react/typescript 大師尋求幫助
type Props = {
children: JSX.Element;
};
const ControlledWrapper: FC<Props> = ({ children }) => {
const state: { hello: string } = { hello: 'hello' };
return children(state);
};
export default function App() {
return (
<ControlledWrapper>
{({ hello }) => <div>{hello}</div>}
</ControlledWrapper>
);
}
uj5u.com熱心網友回復:
將 的型別更改為children回傳 React 元素的函式:
type Props = {
children: (ctx: { hello: string }) => ReactElement;
};
現在,當您使用該組件時,您將獲得正確的型別:
{({ hello }) => <div>{hello}</div>} // 'hello' is a string
在這里試試這個。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/477919.html
