gpt4 book ai didi

javascript - ReactJS:将我自己的 Prop 添加到另一个组件

转载 作者:行者123 更新时间:2023-12-03 11:27:11 24 4
gpt4 key购买 nike

是否可以(甚至是一个好主意)将我自己的 Prop 添加到另一个 React 组件中,例如:

<SomeCustomComponent myOwnParam={handler}>

最佳答案

正如 Tyrsius 所提到的,它实际上取决于 SomeCustomComponent 的实现。如果组件没有在任何地方使用 myOwnParam 属性,传递它不会完成任何事情。另一方面,一些 React 组件可能会使用 JSX 传播属性来引用代码中未直接枚举的 props。

例如,SomeCustomComponent 的以下实现会将您的 myOwnParam 属性传递给它的子 div:

class SomeCustomComponent extends React.Component {
constructor(props) {
super(props);
}

render() {
var {customComponentProp, ...other } = this.props;
return (
<div customComponentProp={customComponentProp} {...other}></div>
);
}
}

所以,这又取决于 SomeCustomComponent 的实现会发生什么。

有关更多详细信息,请参阅传输 Prop 文档:https://facebook.github.io/react/docs/transferring-props.html

关于javascript - ReactJS:将我自己的 Prop 添加到另一个组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34161737/

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