Manon.icu

I'm here to make you a better developer by teaching you everything I know about building for the web.

Published 2021-04-17

React Props

React 组件可以接受传递给它们的数据,称为 props

function App() {
  return <User name="John Doe" />
}

function User(props) {
  return <h1>Hello, {props.name}</h1> // Hello, John Doe!
}

props 可以是任何类型,但是最好是对象。

同样也可以使用解构方式获取仅使用的属性,比如:

function App() {
  return <User name="John Doe" />
}

function User({name}) {
  return <h1>Hello, {name}!</h1> // Hello, John Doe!
}