Components make life easier(组件复用让生活更美好)

简介: Components and PropsConceptually, components are like JavaScript functions. They accept arbitrary inputs (called “props”) and return React elements describing what should appear on the screen.

Components make life easier(组件复用让生活更美好)



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的渲染到页面上的过程:


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


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


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


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


Components可以组合使用


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


tips : Props are Read-Only


目录
相关文章
|
7月前
|
设计模式 编译器 API
【C/C++ Pimpl模式】隐藏实现细节的高效方式 (Pimpl Idiom: An Efficient Way to Hide Implementation Details)
【C/C++ Pimpl模式】隐藏实现细节的高效方式 (Pimpl Idiom: An Efficient Way to Hide Implementation Details)
593 1
|
7月前
ProcessViewer组件没有加载的问题,提示For recursive components, make sure to provide the “name“
ProcessViewer组件没有加载的问题,提示For recursive components, make sure to provide the “name“
47 0
|
7月前
|
JavaScript
解决报错did you register the component correctly? For recursive components, make sure to provide the “na
解决报错did you register the component correctly? For recursive components, make sure to provide the “na
|
JavaScript 前端开发 UED
单文件组件(Single-File Components):Vue.js开发的模块化之道
Vue.js是一款流行的JavaScript框架,以其简洁、灵活和易用而广受欢迎。其中一个Vue.js的强大功能就是单文件组件(Single-File Components),它使得Vue.js应用的开发更加模块化和可维护。在本博客中,我们将深入探讨单文件组件的概念、结构、用法,以及如何利用它们来构建清晰、可复用和高效的Vue.js应用。
202 0
|
前端开发 数据安全/隐私保护
React高阶组件(Higher-Order Components)及其应用
React高阶组件(Higher-Order Components)及其应用
87 0
|
前端开发
react+hook+ts项目总结-ant design input prefix
react+hook+ts项目总结-ant design input prefix
118 0
react+hook+ts项目总结-ant design input prefix
|
前端开发
React+hook+ts+ant design封装一个table的组件
React+hook+ts+ant design封装一个table的组件
318 0
React+hook+ts+ant design封装一个table的组件
|
JavaScript 前端开发 API
前端工作总结126-vue项目报错[Vue warn]: Property “visible“ must be accessed with “$data.visible“ because proper
前端工作总结126-vue项目报错[Vue warn]: Property “visible“ must be accessed with “$data.visible“ because proper
603 0
前端工作总结126-vue项目报错[Vue warn]: Property “visible“ must be accessed with “$data.visible“ because proper
|
Web App开发 JavaScript 前端开发
Modern模式引发qiankun的一场“命案”
前沿:文章的起源在于开发环境中使用qiankun框架,父应用加载子应用遇到一则报错 Failed to load module script: The server responded with a non-JavaScript MIME type of "text/html". Strict MIME type checking is enforced for module scripts per HTML spec 直接的翻译意思就是加载模块脚本失败:服务器以非JavaScript MIME类型“text/html”去响应。而也是这个问题引发了我的思考,到底是什么问题导致?
994 0
Modern模式引发qiankun的一场“命案”