React Tutorial: Basic Concept Of React Component---babel, a translator
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 ReactElement
s. Following the React way, you divide your app into several components according to function and responsibility.
React components and ReactElement
s are used as Virtual DOM, which looks like the markup of real HTML DOM elements.
Virtual DOM (components and ReactElement
s) 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, div
, h3
and p
in browser. But div
, h3
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 ReactElement
s 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 ReactElement
s instead of real HTML DOM elements.
The correct way to set a CSS class name is to use className
property of React components or ReactElement
s.
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的更多相关文章
- React tutorial
https://www.algolia.com Build Unique Search ExperiencesHosted Search API that delivers instant and r ...
- [React Native] Build a Github Repositories component
Nav to Repos component from Dashboard.js: goToRepos(){ api.getRepos(this.props.userInfo.login) .then ...
- [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 ...
- React 与 React Native 底层共识:React 是什么
此系列文章将整合我的 React 视频教程与 React Native 书籍中的精华部分,给大家介绍 React 与 React Native 结合学习的方法,此小节主要介绍 React 的底层原理与 ...
- 2、手把手教React Native实战之从React到RN
###React简介 RN是基于React设计,了解React有助于我们开发RN应用: React希望将功能分解化,让开发变得像搭积木一样,快速而且可维护 React主要有如下3个特点: *作为UI( ...
- (转)2019年 React 新手学习指南 – 从 React 学习线路图说开去
原文:https://www.html.cn/archives/10111 注:本文根据 React 开发者学习线路图(2018) 结构编写了很多新手如何学习 React 的建议.2019 年有标题党 ...
- react用脚手架创建一个react单页面项目,react起手式
官网地址:https://react.docschina.org/ 确保本地安装了Node.js node的版本大于8.10 npm的版本大于5.6 1.在本地的某个位置创建一个文件夹,执行以下 ...
- 使用react搭建组件库:react+typescript+storybook
前期准备 1. 初始化项目 npx create-react-app react-components --template typescript 2. 安装依赖 使用哪种打包方案:webpack/r ...
- React 也就这样 01——React 元素的创建和渲染
React 是一个用于构建用户界面的 JavaScript 库 它包括两个库:react.js 和 react-dom.js react.js:React 的核心库,提供了 React.js 的核心功 ...
随机推荐
- MyBatis系列(三) MyBatis 配置文件
一.properties 此标签的主要作用是引用配置文件,以数据源来举例. 新建mybatis-confing.properties配置文件 mybatis-confing.properties dr ...
- 100道MySQL常见面试题总结
原文链接:https://juejin.im/post/5d351303f265da1bd30596f9 前言 本文主要受众为开发人员,所以不涉及到MySQL的服务部署等操作,且内容较多,大家准备好耐 ...
- 菜刀连接一句话木马出现:`Cannot call assert() with string argument dynamically`错误
前言 逆天还是上学那会玩渗透的,后来工作后就再也没碰了,所以用的工具还是以前经典款,这不,发现出问题了 问题 如果是PHP5则没有问题,如果是PHP7,会出现:Cannot call assert() ...
- windows上 nginx 配置代理服务,配置多域名,以及最简单实现跨域配置
Nginx,不用多说啦,大家都熟悉的不能再熟悉了,它是一款轻量级的高性能Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,最近在本地研究将nginx和resin配合使用,使服务 ...
- oracle 根据字段分组取第一条数据及rank函数说明
当前有这样一个需求,根据外键对子表数据进行分组,取每组中的一条数据就行了,如图: 如:COMMANDID = 26的有两条,只取一条数据. sql语句: select * from(select SY ...
- 两数相加(C#数据结构和算法练习)
两数相加 给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将这两个数相加起来,则会返回一个新的链表来表 ...
- CodeForces 536D Tavas in Kansas
洛谷题目页面传送门 & CodeForces题目页面传送门 A和B在一张无向连通图\(G=(V,E),|V|=n,|E|=m\)上玩一个游戏,节点\(i\)有一个权值\(v_i\).A.B分别 ...
- 【转载】C#使用InsertRange方法往ArrayList集合指定位置插入另一个集合
在C#的编程开发中,ArrayList集合是一个常用的非泛型类集合,ArrayList集合可存储多种数据类型的对象.在实际的开发过程中,我们可以使用InsertRange方法在ArrayList集合指 ...
- Excel 逐条导入Mysql(数据更新)
其实,我的业务流程是, 先读取excel/csv -> pandas 数据清洗 -> 导入Mysql, 一般是做一个表append 或者是 if exist -> replace的操 ...
- python json dumps与loads
json.dumps() 是将一个Python数据结构转换为一个JSON编码的字符串 json.loads() 是将一个JSON编码的字符串转换为一个Python数据结构 一般要求当要字符串通过l ...