Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Typescript React.ComponentProps未获取道具_Javascript_Reactjs_Typescript_Styled Components - Fatal编程技术网

Javascript Typescript React.ComponentProps未获取道具

Javascript Typescript React.ComponentProps未获取道具,javascript,reactjs,typescript,styled-components,Javascript,Reactjs,Typescript,Styled Components,在props中找不到boxProp 图书馆版本: ├─ react@16.13.1 └─ 风格-components@5.2.1 编辑:这将不起作用,因为您实际上并没有将任何道具传递给您的Box组件,第一行所做的只是注释您的Box组件可以使用哪些道具-因此React在使用ComponentProps方法时没有要提取哪些道具的上下文 鉴于您自己正在注释道具类型,解决此问题的一个简单方法是执行以下操作: type BoxProps = { boxProp: boolean } const Box

props
中找不到
boxProp

图书馆版本:

├─ react@16.13.1

└─ 风格-components@5.2.1


编辑:

这将不起作用,因为您实际上并没有将任何道具传递给您的
Box
组件,第一行所做的只是注释您的
Box
组件可以使用哪些道具-因此React在使用
ComponentProps
方法时没有要提取哪些道具的上下文

鉴于您自己正在注释道具类型,解决此问题的一个简单方法是执行以下操作:

type BoxProps = { boxProp: boolean }

const Box = styled('div')<BoxProps>

const MyBox: React.FC<BoxProps> = ({ boxProp }) => {
  return (
    <Box boxProp={boxProp} />
  )
}
type-BoxProps={boxProp:boolean}
常量框=样式化('div')
常量MyBox:React.FC=({boxProp})=>{
返回(
)
}

如果有帮助,请告诉我

我最后一次尝试解决这个问题:

type OwnProps = { boxProps?: boolean };

const Box = styled.div<OwnProps>``;

type CustomBoxProps = StyledComponentProps<'div', DefaultTheme, OwnProps, never>;

export const MyBox: React.FC<CustomBoxProps> = (props) => {
  return (<Box {...props} />);
};
键入OwnProps={boxProps?:boolean};
const-Box=styled.div``;
键入CustomBoxProps=StyledComponentProps;
导出常量MyBox:React.FC=(道具)=>{
返回();
};
不幸的是,必须使用
StyledComponentProps
<代码>反应。ComponentProps与样式化组件接口不兼容。他们这方面肯定有问题


如果这能解决您的问题,请告诉我,谢谢您的解释。这是我之前所做的,但我想推断出不会公开
styled.div
道具的道具,这是这里需要的,您需要使用这些导入,顺便说一句,导入styled,{StyledComponentProps,DefaultTheme}来自“styled components”只是为了让它更能自我解释。我检查了你的代码沙盒,我的示例在那里工作,所以它应该适合你的用例。谢谢你的回答,但我可能不知道哪个样式元素在
框的后面,这意味着
'div'
属于
StyledComponentProposts,您可能没有按照预期的方式使用styled components UI。如果它解决了问题,你能投票吗?因为这显然是一个有效的解决方案