react里面props到底是什么,怎么用。

概念

官网上是这么说的:

When React sees an element representing a user-defined component, it passes JSX attributes to this component as a single object. We call this object “props”.
意思是:当React看到表示用户定义组件的元素时,它会将JSX属性作为单个对象传递给此组件。我们称这个对象为“道具”。


function Welcome(props) {
  return <h1>Hello, {props.name}</h1>;
}//声明welcome组件,通过props.name使用传递过来的属性。
const element = <Welcome name="Sara" />;//使用welcome组件,将JSX属性作为单个对象传递给组件
ReactDOM.render(
  element,
  document.getElementById('root')
);

猜你喜欢

转载自blog.csdn.net/lncci/article/details/87934002
今日推荐