gpt4 book ai didi

javascript - 跨共享组件的 React js 和 PropTypes 重复

转载 作者:数据小太阳 更新时间:2023-10-29 04:05:13 25 4
gpt4 key购买 nike

我已经创建了一些 React 组件,因此,父级...获得了一些 Prop ...

随后的每个 child 都会使用这些 Prop 中的大部分,然后是 child 的 child 。

**--> Parent**
(required props)
**-------> child**
(required props)
**-------> child**
(required props)
**------------> sub child**
(required props)
**------------> sub child**

那些“必需的 Prop ”对于所有这些组件都是相同的。似乎过分了,每当我更新 Parent 中的 Prop 时,我就必须进入所有这些 child 并更新到(如果需要)。当然,它们是必需的和需要的,但好奇是否有不需要重复的快捷方式/或实现。有什么想法吗?

谢谢

最佳答案

您可以将您的 Prop 类型存储在一个对象中,您将其合并到每个组件的 propTypes 中:

var requiredPropTypes = {
foo: ...,
bar: ...
};

var ComponentA = React.createClass({
propTypes: {
...requiredPropTypes,
// ComponentA's prop types follow
// ...
},
// ...
});

var ComponentB = React.createClass({
propTypes: {
...requiredPropTypes,
// ComponentB's prop types follow
// ...
},
// ...
});

propTypes 的值只是一个对象。如何构建该对象完全取决于您。

关于javascript - 跨共享组件的 React js 和 PropTypes 重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30265327/

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