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
Reactjs 导出部分道具类型以在组件中重用';道具_Reactjs_Eslint_React Props - Fatal编程技术网

Reactjs 导出部分道具类型以在组件中重用';道具

Reactjs 导出部分道具类型以在组件中重用';道具,reactjs,eslint,react-props,Reactjs,Eslint,React Props,下面的代码片段演示了两种道具类型,我想将它们提取到一个单独的共享文件中,以便在其他组件中重用 /Mycomponent.js MyComponent.propTypes={ 标题:PropTypes.string, description:PropTypes.string, image:PropTypes.string, } MyComponent.defaultProps={ 标题:“某些默认标题”, 描述:“某些默认描述”, 图像:'./image.jpg' } 我想做一个通用的share

下面的代码片段演示了两种道具类型,我想将它们提取到一个单独的共享文件中,以便在其他组件中重用

/Mycomponent.js

MyComponent.propTypes={
标题:PropTypes.string,
description:PropTypes.string,
image:PropTypes.string,
}
MyComponent.defaultProps={
标题:“某些默认标题”,
描述:“某些默认描述”,
图像:'./image.jpg'
}
我想做一个通用的share props变量,只包含标题和描述。为了做到这一点,我创建了一个共享道具文件,并提取了我想重复使用的道具到该文件中,如下面的示例所示

/shared props.js

const fooProps={
标题:PropTypes.string,
description:PropTypes.string,
}
现在,我想在/Mycomponent.js中重复使用它:

从“/shared props.js”导入{fooProps};
MyComponent.propTypes={
…fooProps,//{
// ...
现在页面呈现很好。这只是ESLint抛出的错误吗?还是我应该以不同的方式导出道具


更新:按如下方式使用时,它确实有效:

/shared props.js

const fooProps=PropTypes.shape({
标题:PropTypes.string,
description:PropTypes.string,
});
/Mycomponent.js:

从“/shared props.js”导入{fooProps};
MyComponent.propTypes={

foo:fooProps,//如果您分解
fooProps
MyComponent.propTypes={…fooProps,image:propTypes.string,};
)linting错误是否仍然出现?现在您正在声明一个名为
fooProps
的道具。很好。尝试了它,但似乎不起作用。这是eslint和规则的一个限制。一个解决方案是在导入时分解fooProps,并手动添加每个道具
import{{title,description}从…
MyComponent.propTypes={title,description,image:..
如果您分解
fooProps
MyComponent.propTypes={…fooProps,image:propTypes.string,};
)linting错误是否仍然出现?现在您正在声明一个名为
fooProps
的道具。很好。尝试了它,但似乎不起作用。这是eslint和规则的一个限制。一个解决方案是在导入时分解fooProps,并手动添加每个道具
import{{title,description}从…
MyComponent.propTypes={标题、描述、图像:…