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


目录
相关文章
|
4月前
|
JavaScript 前端开发
Ant Design Vue 从V3到V4的升级原来进行了这个技术的调整
Ant Design Vue 从V3到V4的升级原来进行了这个技术的调整,你有了解吗?
162 0
|
8月前
|
前端开发
前端项目实战柒拾陆react-admin+material ui-踩坑-控制路由的关键是baseName
前端项目实战柒拾陆react-admin+material ui-踩坑-控制路由的关键是baseName
65 0
|
7月前
|
JavaScript 前端开发 UED
单文件组件(Single-File Components):Vue.js开发的模块化之道
Vue.js是一款流行的JavaScript框架,以其简洁、灵活和易用而广受欢迎。其中一个Vue.js的强大功能就是单文件组件(Single-File Components),它使得Vue.js应用的开发更加模块化和可维护。在本博客中,我们将深入探讨单文件组件的概念、结构、用法,以及如何利用它们来构建清晰、可复用和高效的Vue.js应用。
80 0
|
8月前
|
前端开发
前端项目实战捌拾react-admin+material ui-踩坑-List的用法之actions中<FilterButton/>需要进行配置
前端项目实战捌拾react-admin+material ui-踩坑-List的用法之actions中<FilterButton/>需要进行配置
37 0
|
8月前
|
前端开发
前端项目实战柒拾捌react-admin+material ui-踩坑-List的用法之actions
前端项目实战柒拾捌react-admin+material ui-踩坑-List的用法之actions
43 0
|
8月前
|
前端开发 索引
【React工作记录七十九】React+hook+ts+ant design封装一个具有动态表格得页面
【React工作记录七十九】React+hook+ts+ant design封装一个具有动态表格得页面
83 0
|
10月前
|
前端开发 数据安全/隐私保护
React高阶组件(Higher-Order Components)及其应用
React高阶组件(Higher-Order Components)及其应用
56 0
|
10月前
|
前端开发
|
10月前
|
前端开发
前端学习笔记202304学习笔记第十一天-vue3.0-基于provide向下共享数据
前端学习笔记202304学习笔记第十一天-vue3.0-基于provide向下共享数据
28 0
|
前端开发
#yyds干货盘点 【React工作记录二十四】ant design form赋值问题
#yyds干货盘点 【React工作记录二十四】ant design form赋值问题
97 0
#yyds干货盘点 【React工作记录二十四】ant design form赋值问题