简体   繁体   English

传递 props 时何时使用 this.prop?

[英]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?有人可以澄清一下吗?

  1. 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.每次我们导入。 在此处输入图像描述

  2. 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 相同。 在此处输入图像描述

  3. 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关键字访问stateprops

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})意味着属性termonTermSubmitonTermChange正在从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.

相关问题 将道具添加到构造函数中作为this.prop - Adding props to constructor as this.prop 传承道具,传承时会计算其值 - Passing down a prop who's value is calculated when passed down this.prop不是函数吗? - this.prop is not a function? 如何在传递道具时使用 TypeScript 实现“as”道具? - How can I implement a "as" prop with TypeScript while passing down the props? c.easing [this.options.specialEasing && this.options.specialEasing [this.prop] || a]在使用JQuery Easing插件时不是功能 - c.easing[this.options.specialEasing && this.options.specialEasing[this.prop] || a] is not a function when using JQuery Easing plugin 获取TypeError:_red2.props.handleClick不是一个函数,当将redux prop从容器传递到react中的表示组件时 - Getting TypeError: _this2.props.handleClick is not a function when passing redux prop from container to presentational component in react 在传递道具时无需在React中列出它们,如何获得对所有元素道具的访问权限? - How to gain access to all of an elements props when passing down props without having to list them in React? TypeError:未定义不是this.prop对象 - TypeError: Undefined is not an object this.prop this.prop没有正确 - this.prop is not getting though right 嵌套时将道具传递给 Outlet,以便我可以使用 componentDidUpdate - Passing props to Outlet when nesting so I could use componentDidUpdate
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM