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


目录
相关文章
|
2月前
|
JavaScript 前端开发
Ant Design Vue 从V3到V4的升级原来进行了这个技术的调整
Ant Design Vue 从V3到V4的升级原来进行了这个技术的调整,你有了解吗?
264 0
|
10月前
|
JavaScript 前端开发 UED
单文件组件(Single-File Components):Vue.js开发的模块化之道
Vue.js是一款流行的JavaScript框架,以其简洁、灵活和易用而广受欢迎。其中一个Vue.js的强大功能就是单文件组件(Single-File Components),它使得Vue.js应用的开发更加模块化和可维护。在本博客中,我们将深入探讨单文件组件的概念、结构、用法,以及如何利用它们来构建清晰、可复用和高效的Vue.js应用。
124 0
|
11月前
|
前端开发 索引
【React工作记录七十九】React+hook+ts+ant design封装一个具有动态表格得页面
【React工作记录七十九】React+hook+ts+ant design封装一个具有动态表格得页面
102 0
|
前端开发 数据安全/隐私保护
React高阶组件(Higher-Order Components)及其应用
React高阶组件(Higher-Order Components)及其应用
61 0
|
前端开发
|
前端开发
【React工作记录七十七】React+hook+ts+ant design封装一个input和select搜索的组件
React+hook+ts+ant design封装一个input和select搜索的组件
145 0
react+hook+ts项目总结-ant design input prefix
react+hook+ts项目总结-ant design input prefix
96 0
react+hook+ts项目总结-ant design input prefix
|
前端开发
前端hook项目moblie总结笔记-ant design动态数据赋值
前端hook项目moblie总结笔记-ant design动态数据赋值
62 0
|
前端开发
前端hook项目moblie总结笔记-ant design动态数据赋值
前端hook项目moblie总结笔记-ant design动态数据赋值
65 0
|
前端开发
React+hook+ts+ant design封装一个table的组件
React+hook+ts+ant design封装一个table的组件
271 0
React+hook+ts+ant design封装一个table的组件