简体   繁体   English

无状态功能组件 - React

[英]Stateless Functional Component - React

I am new to React and I am doing some tutorials online.我是 React 的新手,我正在网上做一些教程。 Now I have task like this: If the Component in the following code is a Stateless Functional Component, what expression would you write to access the items prop inside the Component?现在我有这样的任务:如果下面代码中的组件是一个无状态功能组件,你会写什么表达式来访问组件内的 items 属性?

<IngredientList items={ingredient.items} />

I have no idea what is correct answer and why :/ Would you be so kind to help me ?我不知道什么是正确答案以及为什么:/你能帮我吗?

If it's stateless props.items .如果是无状态的props.items If its a class this.props.items如果它是一个类this.props.items

Stateless functional components typically look something like this.无状态功能组件通常看起来像这样。

Passing just a single prop to a child would like this.只将一个道具传递给孩子就是这样。

const ChildComponent = ({ someProp }) => (
   <div>
      <h1>Child Component {someProp}</h1>
   </div>
)

Passing all of the parents props to a child would like like this.将所有父母道具传递给孩子会像这样。

const ChildComponent = (props) => (
   <div>
      <h1>Child Component {props.someProp}</h1>
   </div>
)

You can access all your props by using props.itemName.您可以使用 props.itemName 访问所有道具。 Ex: By simply typing props.items例如:只需输入 props.items

Your functional component will look something like below,您的功能组件将如下所示,

const IngredientList = props => {
  // here you get all the props passed to IngredientList
  const items = props.items.map(item => <div>item</div>);

  return <div>items</div>
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM