gpt4 book ai didi

reactjs - TypeScript 错误将 Prop 传递给样式组件

转载 作者:搜寻专家 更新时间:2023-10-30 20:42:05 28 4
gpt4 key购买 nike

我很难解决这个 TypeScript 问题。

...message: 'Type '{ show: boolean; children: Element; }' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes<Component<ThemedOuterStyledProps<HTMLProps<HTMLDiv...'.
Property 'show' does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes<Component<ThemedOuterStyledProps<HTMLProps<HTMLDiv...'.'

我正在使用 React + 样式化组件 + TypeScript。如果我有这样的样式组件:

const Component = styled.div`
opacity: ${props => props.show ? 1 : 0}
`

我的 React 组件如下所示:

const ReactComponent = (props: { appLoading: boolean }) => (
<Component show={appLoading} />
)

我是 TypeScript 的新手,但我想我需要以某种方式在组件上定义 show prop?

最佳答案

show 属性赋予类型的一种方法可能是这样的 -

const Component = styled.div`
opacity: ${(props: {show: boolean}) => props.show ? 1 : 0}
`

刚刚将 () 添加到“props: {show: boolean}”

关于reactjs - TypeScript 错误将 Prop 传递给样式组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44090290/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com