我在 React native 中使用樣式化的組件。我有一個名為 isTablet 的布林值。
我創建了一個名為 IdTextInput 的組件,它使用 TextInput 標記。此時,我將名為 platform 的 isTablet 布林值作為來自 styled-component 的 props 傳遞。
但是,如果我這樣做,它會起作用,但會出現型別警告。
Property 'platform' does not exist on type 'ThemedStyledProps<TextInputProps & RefAttributes<TextInput>, DefaultTheme>'
No overload matches this call.
Overload 1 of 2, '(props: Omit<Omit<TextInputProps & RefAttributes<TextInput>, never> & Partial<Pick<TextInputProps & RefAttributes<TextInput>, never>>, "theme">....
所以我在 props 中放了一個布爾型別,但是沒有用。如何修復我的代碼?下面是我的代碼
const IdTextInput = styled.TextInput`
width: ${(props) =>
props.platform ? 50 : 100}%;
`;
const isTablet = DeviceInfo.isTablet();
return (
<IdTextInput
platform={isTablet}
/>
)
uj5u.com熱心網友回復:
添加檔案中提到的自定義道具。
https://styled-components.com/docs/api#using-custom-props
interface Props {
platform: boolean;
}
const IdTextInput = styled.TextInput<Props>`
width: ${(props) => (props.platform ? 50 : 100)}%;
`;
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/525766.html
標籤:打字稿反应式样式化组件
