Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Components and Props #4

Closed
pluscai opened this issue Apr 28, 2020 · 0 comments
Closed

Components and Props #4

pluscai opened this issue Apr 28, 2020 · 0 comments

Comments

@pluscai
Copy link
Owner

pluscai commented Apr 28, 2020

4. Components and Props

Conceptually, components are like JavaScript functions. They accept arbitrary inputs (called “props”) and return React elements describing 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的渲染到页面上的过程:

    1. we call ReactDOM.render() the <Welcome name="Sara" /> element.

    2. React calls the Welcome component with {name: 'Sara'} as the props.

    3. Our Welcome component returns a Hello, Sara element as the result

    4. React DOM efficiently updates the DOM to match Hello, Sara.

  • Components可以组合使用

    Components可以组合使用,也可以将常用部分提取出来作为components

tips : Props are Read-Only

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant