Components and Props
Conceptually,
componentsare like JavaScript functions. They accept arbitrary inputs (called “props”) and returnReact elementsdescribing what should appear on the screen.
-
Component的两种写法:
// Function Components function Welcome(props) { return <h1>Hello, {props.name}</h1>; } // => 等价于下面写法 // Class Components class Welcome extends React.Component { render() { return <h1>Hello, {this.props.name}</h1>; } } -
渲染Component的机制
官网例子:
function Welcome(props) { return <h1>Hello, {props.name}</h1>; } const element = <Welcome name="Sara" />; ReactDOM.render( element, document.getElementById('root') );分析component的渲染到页面上的过程:
-
we call
ReactDOM.render()the<Welcome name="Sara" />element. -
React calls the
Welcomecomponent with{name: 'Sara'}as the props. -
Our
Welcomecomponent returns aHello, Saraelement as the result -
React DOM efficiently updates the DOM to match
Hello, Sara.
-
-
Components可以组合使用
Components可以组合使用,也可以将常用部分提取出来作为components
tips : Props are Read-Only
本文深入探讨了React中组件的概念,包括Function Components和Class Components的写法,Props的读取方式,以及组件如何通过Props接受输入并返回描述屏幕显示的React元素。同时,文章详细分析了组件从渲染到页面的过程,以及组件如何被组合使用。
910

被折叠的 条评论
为什么被折叠?



