Javascript 如何将forwardedAs道具与样式化组件一起使用?将forwardedAs道具与typescript一起使用
以下是Javascript 如何将forwardedAs道具与样式化组件一起使用?将forwardedAs道具与typescript一起使用,javascript,reactjs,typescript,styled-components,react-props,Javascript,Reactjs,Typescript,Styled Components,React Props,以下是forwardedAsprop:上的文档 正如你所看到的,它不是很详细,也没有说明如何正确使用这个道具 我的问题是:如何访问通过forwardedAs发送的道具?如何为这些forwardedAsprops定义类型 我可以通过…rest参数访问forwardedAs道具,但我需要为这些道具定义类型,因为我还将样式化组件与Typescript一起使用 下面是我的代码示例: // Button.jsx const myPropsToForward = { href: 'https://som
forwardedAs
prop:上的文档
正如你所看到的,它不是很详细,也没有说明如何正确使用这个道具
我的问题是:如何访问通过forwardedAs发送的道具?如何为这些forwardedAs
props定义类型
我可以通过…rest
参数访问forwardedAs
道具,但我需要为这些道具定义类型,因为我还将样式化组件与Typescript一起使用
下面是我的代码示例:
// Button.jsx
const myPropsToForward = {
href: 'https://somewebsite.com',
// ...more props
}
const Button = styled.button`
// ...button styles
`
const myComponent = () => (
<Button
as={Link}
to={ctaLink}
forwardedAs={myPropsToForward}
/>
)
//Button.jsx
常量myPropsToForward={
href:'https://somewebsite.com',
//…更多道具
}
const Button=styled.Button`
//…按钮样式
`
常量myComponent=()=>(
)
//Link.jsx
常量链接=({
转发,
休息
}) => {
//如何从这里访问forwardedAs道具?
返回(
)
}
在这里,我需要能够访问通过forwardedAs
prop发送的Link
组件中的道具,但是没有关于如何操作的文档。如果我可以访问forwardedAs
prop,那么我就可以为链接
组件定义适当的类型。我不想依赖于…rest
参数,因为我无法为此定义类型
提前谢谢。转发
forwardedAs
道具不用于传递道具。它实际上是将作为道具传递给链中的下一项。考虑这个例子:
const Button = styled.button`
padding: 20px;
`;
const Link = (props: any) => { // not properly typed
return <Button {...props} />;
};
const MyLink = styled(Link)`
background-color: blue;
`
const MyComponent = () => (
<MyLink forwardedAs={"div"}>
Text
</MyLink>
);
作为一个额外的问题,我如何定义链接组件上这个forwardedAs道具的类型?
const Button = styled.button`
padding: 20px;
`;
const Link = (props: any) => { // not properly typed
return <Button {...props} />;
};
const MyLink = styled(Link)`
background-color: blue;
`
const MyComponent = () => (
<MyLink forwardedAs={"div"}>
Text
</MyLink>
);
const myPropsToForward = {
href: "https://somewebsite.com"
// ...more props
};
const Button = styled.button`
background: yellow;
padding: 20px;
`;
const MyComponent = () => (
<Button as="a" {...myPropsToForward}>
Text
</Button>
);