Getting started with react.js: basic concept of React component

1 What is React.js

React, or React.js is an open source javascript framework from Facebook. React.js is ideal for doing view rendering work in large scale or single page application (SPA). That is, React works as the V in MVC.

2 How React works

The main concept of React is component. A React component can be composed of React components and ReactElements. Following the React way, you divide your app into several components according to function and responsibility.

React components and ReactElements are used as Virtual DOM, which looks like the markup of real HTML DOM elements.

Virtual DOM (components and ReactElements) syntax will be compiled and translated into plain javascript codes, which will render real HTML DOM elements in browser.

3 Write a React component

As mentioned above, two things are required to write and run a React component.

  • React.js
  • babel, a translator, can translate ECMAScript 6 codes and Virtual DOM syntax to ECMAScript 5 codes, which can be run in all modern browsers.

For simplicity, we're going to use React and babel scripts hosted by free CDN.

 

Create a HelloWorld component which is responsible for rendering a HTML h3 tag and a HTML p tag. Then mount the HelloWorld component to div tag whose id is app.

 

Pay attention to type attribute of script tag. Its vaule is text/babel, which means that the codes should be processed and translated by babel.

Note: The net result of running above code snippets is that React will render real HTML DOM tags, divh3and p in browser. But divh3 and p in above codes are not real HTML DOMs, they are also virtual DOMs. They are also React elements, that is ReactElement exactly in React's eyes.

Take the <h3>Hello</h3> element in above render() function as an example, React.js will create a ReactElement essentially. Equivalently, the behind the scenes code are as follows.

 

As you can see, a react component can be embedded as a virtual DOM within another component. For another example, you can give a root component, e.g. App, and then use all sub components and ReactElements within this root.

 

4 CSS class and inline styles of React component

The usual way of adding CSS class name and inline styles will not take effect in React component.

 

The inline style color:blue; and CSS class greet not only won't work, but will raise javascript error. (See it in javascript console of developer tool in your browser.)

Just as I have said, h3 and p here are ReactElements instead of real HTML DOM elements.

The correct way to set a CSS class name is to use className property of React components or ReactElements.

Likewise, inline CSS styles should be wrapped as an object and passed to the style property of React components.

 

Alternatively, if the inline styles are long and complex, you can predefine styles as a variable.

 

Note that the CSS property name should be written in CamelCase way, just as the way in plain javascript.

 

5 Browser plugin: React developer tool

You can install browser plugin React developer tool for purpose of debugging.

https://www.codevoila.com/post/38/react-tutorial-basic-concept-of-react-component

React Tutorial: Basic Concept Of React Component---babel, a translator的更多相关文章

  1. React tutorial

    https://www.algolia.com Build Unique Search ExperiencesHosted Search API that delivers instant and r ...

  2. [React Native] Build a Github Repositories component

    Nav to Repos component from Dashboard.js: goToRepos(){ api.getRepos(this.props.userInfo.login) .then ...

  3. [React] Preventing extra re-rendering with function component by using React.memo and useCallback

    Got the idea form this lesson. Not sure whether it is the ncessary, no othere better way to handle i ...

  4. React 与 React Native 底层共识:React 是什么

    此系列文章将整合我的 React 视频教程与 React Native 书籍中的精华部分,给大家介绍 React 与 React Native 结合学习的方法,此小节主要介绍 React 的底层原理与 ...

  5. 2、手把手教React Native实战之从React到RN

    ###React简介 RN是基于React设计,了解React有助于我们开发RN应用: React希望将功能分解化,让开发变得像搭积木一样,快速而且可维护 React主要有如下3个特点: *作为UI( ...

  6. (转)2019年 React 新手学习指南 – 从 React 学习线路图说开去

    原文:https://www.html.cn/archives/10111 注:本文根据 React 开发者学习线路图(2018) 结构编写了很多新手如何学习 React 的建议.2019 年有标题党 ...

  7. react用脚手架创建一个react单页面项目,react起手式

    官网地址:https://react.docschina.org/ 确保本地安装了Node.js node的版本大于8.10    npm的版本大于5.6 1.在本地的某个位置创建一个文件夹,执行以下 ...

  8. 使用react搭建组件库:react+typescript+storybook

    前期准备 1. 初始化项目 npx create-react-app react-components --template typescript 2. 安装依赖 使用哪种打包方案:webpack/r ...

  9. React 也就这样 01——React 元素的创建和渲染

    React 是一个用于构建用户界面的 JavaScript 库 它包括两个库:react.js 和 react-dom.js react.js:React 的核心库,提供了 React.js 的核心功 ...

随机推荐

  1. javascript 忍者秘籍读书笔记

    书名 "学徒"=>"忍者" 性能分析 console.time('sss') console.timeEnd('sss') 函数 函数是第一类对象 通过字 ...

  2. 不能随便用get和set

    有些对象呢,保存一半.如果你只提供get和set,那么备份不了数据. previousState的get和set还是最新的 wtforms InputRequired: DataRequired: i ...

  3. ElasticSearch中碰到的C10K问题

    Elasticsearch基于Netty解决C10K问题背后的原理是JAVA NIO中的IO多路复用机制,涉及到三大"组件":SelectableChannel.Selector. ...

  4. 12. Scala模式匹配

    12.1 match 12.1.1 基本介绍 Scala中的模式匹配类似于Java中的switch语法,但是更加强大 模式匹配语法中,采用match关键字声明,每个分支采用case关键字进行声明,当需 ...

  5. 【题解】Luogu P5327 [ZJOI2019]语言

    原题传送门 看到这种树上统计点对个数的题一般是线段树合并,这题也不出意外 先对这棵树进行树剖,对于每次普及语言,在\(x,y\)两点的线段树上的\(x,y\)两位置打\(+1\)标记,在点\(fa[l ...

  6. - Gradle 翻译 Analyzer APK文件分析 MD

    目录 目录 APK文件分析 使用 APK 分析器分析您的编译版本 查看文件和大小信息 查看 AndroidManifest.xml 查看 DEX 文件 过滤DEX文件树视图 加载 Proguard 映 ...

  7. 使用位运算实现int32位 整数的加减乘除

    我觉得比较难想的是加法吧. 首先加法,脑海中脑补二进制加法,相同位相加,超过2 ,则进1,留0 那么用位运算怎么实现呢?其实理解了异或和与操作,就很容易想出来了. 我觉得异或操作和与操作完全就是实现加 ...

  8. vue前端实战注意事项

    1. vue前端实战注意事项 1.1. 预备 1.1.1. Eslint 这是个语法检查工具,我用webstorm作为开发的ide,这个语法检查还是太严格了,一个空格啥的都会报错,对新手来讲还是建议关 ...

  9. SpringBoot+Security+MyBatis+ES+MQ+Redis+Docker+Vue的电商系统

    今天鹏哥给大家推荐的项目是一套电商系统,包括前台商城系统及后台管理系统,基于SpringBoot+MyBatis实现. 前台商城系统包含首页门户.商品推荐.商品搜索.商品展示.购物车.订单流程.会员中 ...

  10. Java面试通关要点 汇总集【最终版】

    本文转载自公众号:服务端思维,阅读大约需要7分钟.梁兄的知识储备很丰富,组织的知识星球里也是干货十足,平常还会有技术研习等活动,欢迎关注. 首先,声明下,以下知识点并非阿里的面试题.这里,笔者结合自己 ...