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


目录
相关文章
|
6月前
|
存储 测试技术 UED
Qt中实现界面回放的艺术:从理论到代码“ (“The Art of Implementing UI Playback in Qt: From Theory to Code
Qt中实现界面回放的艺术:从理论到代码“ (“The Art of Implementing UI Playback in Qt: From Theory to Code
151 1
|
6月前
ProcessViewer组件没有加载的问题,提示For recursive components, make sure to provide the “name“
ProcessViewer组件没有加载的问题,提示For recursive components, make sure to provide the “name“
43 0
|
JavaScript 前端开发 UED
单文件组件(Single-File Components):Vue.js开发的模块化之道
Vue.js是一款流行的JavaScript框架,以其简洁、灵活和易用而广受欢迎。其中一个Vue.js的强大功能就是单文件组件(Single-File Components),它使得Vue.js应用的开发更加模块化和可维护。在本博客中,我们将深入探讨单文件组件的概念、结构、用法,以及如何利用它们来构建清晰、可复用和高效的Vue.js应用。
198 0
构建 Rack-based 框架 (Rails/Grape/Sinatra) 的几个 Tricks
构建 Rack-based 框架 (Rails/Grape/Sinatra) 的几个 Tricks
Dva中在effects中获取state的值
Dva中在effects中获取state的值
333 0
react+hook+ts项目总结-ant design input prefix
react+hook+ts项目总结-ant design input prefix
117 0
react+hook+ts项目总结-ant design input prefix
|
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
593 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”去响应。而也是这个问题引发了我的思考,到底是什么问题导致?
992 0
Modern模式引发qiankun的一场“命案”
|
前端开发 JavaScript 程序员
造一个 react-error-boundary 轮子
朋友们好啊,我是海怪,刚才老板对我说:海怪,发生甚么事了,怎么页面白屏了?我说:怎么回事?给我发了几张截图。我打开控制台一看:哦!原来是昨天,有个后端年轻人,说要和我联调接口,我说:可以。然后,我说:小兄弟,你的数据尽量按我需要的格式来:踏不服气,他说你这个没用,我说我这个有用,这是规范,传统前后端联调返回数据是要讲规范的,对项目质量的提高可以起到四两拨千斤的作用。100多万行代码的系统,只要有了类型规范,都不会轻易崩溃。他说试试,我说行。 我请求刚发出去,他的数据,啪!的一下就返回了!很快啊!!
造一个 react-error-boundary 轮子
|
JavaScript
vue cli3.0创项目报错‘This may cause things to work incorrectly. Make sure to use the same version for b’
vue cli3.0创项目报错‘This may cause things to work incorrectly. Make sure to use the same version for b’
vue cli3.0创项目报错‘This may cause things to work incorrectly. Make sure to use the same version for b’