[英]When to use this.prop when passing down props?
I'm a beginner in JS/React and I am trying to wrap my head around the concept of when to use this.prop or just passing props down without this in React.我是 JS/React 的初学者,我正试图围绕何时使用 this.prop 或仅在 React 中不使用 this 传递 props 的概念。
In some sources, the coder always uses this.prop while in others this seems to be unnecessary.在某些来源中,编码器总是使用 this.prop,而在其他来源中,这似乎是不必要的。 Could anyone please clarify?有人可以澄清一下吗?
In this example, we are passing props username, authed, logout, and header from apps to hello.在这个例子中,我们将 props username、authed、logout 和 header 从应用程序传递给 hello。 To do so, we use this.props.为此,我们使用 this.props。 everytime we import.每次我们导入。
However, in this example (React Native), we pass down term, onTermChange, and onTermSubmit from searchscreen to searchbar, without using this.props.但是,在这个示例(React Native)中,我们将 term、onTermChange 和 onTermSubmit 从 searchscreen 传递到 searchbar,而不使用 this.props。 Then I believe we re-defined our props in the TextInput of Search bar so its onEndEditing is the same as the onTermSubmit from the searchscreen.然后我相信我们在搜索栏的 TextInput 中重新定义了我们的道具,因此它的 onEndEditing 与搜索屏幕中的 onTermSubmit 相同。
I thought this.props may be a react syntax which became streamlined in React Native.我认为 this.props 可能是一种反应语法,它在 React Native 中得到了简化。 However, I came across yet another example in React which does not use this.props:但是,我在 React 中遇到了另一个不使用 this.props 的示例:
You will use "this" when you have a class component.当您有 class 组件时,您将使用“this”。 When you have a functional component there is no need, because you're not creating an instance of a class.如果您有一个功能组件,则不需要,因为您没有创建 class 的实例。 All arguments passed to a class are stored into "this.props".所有传递给 class 的 arguments 都存储在“this.props”中。
So in your second example and third example, you have functional components, which take the consts passed to them not as props, but as actual constants with their own names.因此,在您的第二个示例和第三个示例中,您有功能组件,它们将传递给它们的 consts 不是作为道具,而是作为具有自己名称的实际常量。
Hope this helps.希望这可以帮助。
props
is being used in both scenarios.在这两种情况下都使用了props
。 You are just accessing props values differently.您只是以不同的方式访问道具值。 First example uses Class component.第一个示例使用 Class 组件。 That means props
is passed to children as this.props
, where as second example uses stateless/ functional component, which means no this
.这意味着props
作为this.props
传递给孩子,而第二个示例使用无状态/功能组件,这意味着没有this
。 And和
const FunctionalComponent = (props) => {
const {name, onLogin} = props
......
}
is equivalent to相当于
const FunctionalComponent = ({name, onLogin}) => {
.....
}
due to Object Destructuring , hence you are not seeing explicit use of props
in second example.由于Object Destructuring ,因此您在第二个示例中没有看到props
的明确使用。
It's not a React
thing but a JavaScript
thing.这不是React
的东西,而是JavaScript
的东西。
Classes - Here you need to access the state
and props
using the this
keyword.类 - 在这里您需要使用this
关键字访问state
和props
。
Functions - Here you don't need to use this
in React.函数 - 在这里你不需要在 React 中使用this
。 The argument to your function component here are props
so you can directly use props.something
and not this.props.something
.此处 function 组件的参数是props
,因此您可以直接使用props.something
而不是this.props.something
。
In your Searchbar component:-在您的搜索栏组件中:-
const Searchbar = ({term,onTermSubmit,onTermChange})
means that the properties term
, onTermSubmit
and onTermChange
are being destructured from the props
object and so can now be used directly. const Searchbar = ({term,onTermSubmit,onTermChange})
意味着属性term
, onTermSubmit
和onTermChange
正在从props
object 中解构,因此现在可以直接使用。 You don't need to access them like props.term
anymore.您不再需要像props.term
那样访问它们。 They can directly be accessed like term
wherever you want them to be.无论您希望它们在哪里,它们都可以像term
一样直接访问。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.