[英]React.js: Passing props to props?
I am learning React via an online course and have stumbled across a situation where a property is attached to another property like so: 我正在通过在线课程学习React,并偶然发现了一个属性附加到另一个属性的情况,如下所示:
this.props.property01(this.props.property02)
Seeing as the tutor only briefly addresses this line of code I cannot but remain baffled as to what this "concatenation" syntax actually stands for, ie what it causes behind the scenes. 看到导师只是简单地解决了这行代码,我不得不对这种“连接”语法实际代表什么,即它在幕后引起的内容感到困惑。 Coming from Vanilla JS it looks like property02 is being used as an argument for property01 but that seems too simple an answer. 来自Vanilla JS,看起来property02被用作property01的参数,但这似乎太简单了。 That being said, I understand the remaining code quite well. 话虽这么说,我完全理解剩下的代码。
To provide some context I have created this codepen in which the problem I refer to above is given by this.props.onDelete(this.props.whichItem);
为了提供一些上下文,我创建了这个codepen,其中我在上面引用的问题由this.props.onDelete(this.props.whichItem);
. 。
Seeing as I could not find any related questions, I would be grateful for some insightful elaboration on this one. 鉴于我找不到任何相关问题,我将非常感谢对此进行一些深刻的阐述。
The properties of a React component can be functions. React组件的属性可以是函数。 When I see: 当我看见:
this.props.property01(this.props.property02)
I think that: 我觉得:
this.props.property01
is a function. this.props.property01
是一个函数。 this.props.property02
is passed to the function as an argument. this.props.property02
作为参数传递给函数。 This means that the component would be used as: 这意味着该组件将用作:
<SomeComponent property01={(a) => { /* so something with "a" ... */ } property02={"someValue"} />
If property02
is only used to be passed to property01
and nothing else I would prefer something like: 如果property02
仅用于传递给property01
而没有其他任何我喜欢的东西,如:
<SomeComponent property01={(a) => { /* do something with "someValue" ... */ } />
Which means that there is no need for a property called poperty02
. 这意味着不需要名为poperty02
的属性。
Coming from Vanilla JS it looks like property02 is being used as an argument for property01 来自Vanilla JS,看起来property02被用作property01的参数
That's it, you are correct. 就是这样,你是对的。
this.props.property01(this.props.property02) this.props.property01(this.props.property02)
property01 is a method, property02, the argument. property01是一个方法,property02,参数。
A more elaborated explanation for anyone else looking at this: 对于其他人来说,这是一个更详细的解释:
Assume this line is in a component called MyComponent
and property01
is a prop in MyComponent
. 假设这条线是在一个组件调用MyComponent
和property01
处于丙MyComponent
。
The parent component's render() method, would contain something like this: 父组件的render()方法将包含以下内容:
<MyComponent property01={this.someMethod.bind(this)} />
Where someMethod
belongs to that parent component but it becomes available as property01
in MyComponent
(the child component). someMethod
属于该父组件,但它在MyComponent
(子组件)中可用作property01
。
If you can understand the basic react code this question will help you because in that code I passed props and state from parent component to the child component. 如果您能理解基本的反应代码,这个问题将对您有所帮助,因为在该代码中,我将props和状态从父组件传递给子组件。 You can play with it try to do whatever you want. 你可以玩它尝试做你想做的任何事情。
How to pass state from parent compont to child component in route (react-route-dom) reactjs 如何将状态从父compont传递到路径中的子组件(react-route-dom)reactjs
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.