记录一下每天学习的笔记或者工作中的问题
React virtual dom
记录一下每天学习的笔记或者工作中的问题
Home Page: https://qiantaobao.github.io/blog
记录一下每天学习的笔记或者工作中的问题
React virtual dom
source map
当我们在开发环境出现错误,想通过控制台来查找错误是哪一行的时候,就可以用到source map
在webpack中配置如下
devtool: "source-map",
例如我们index.js里面有一行代码拼错
consol.log('hehe')
那么打包后可以在控制台中看到
Uncaught ReferenceError: consol is not defined
at eval (index.js?b635:4)
source map有很多种配置,下面我们来说说常用的几个配置
构建速度 慢
重新构建速度 慢
源码映射,会单独产生一个单独的source map文件,出错会显示当前的行和列,是一个比较大和全的错误调试配置
构建速度 慢
重新构建速度 比较快
不会产生单独的文件,但会显示行和列
构建速度 中等
重新构建速度 比较慢
不会产生列,但是会产生一个单独的映射文件
构建速度 中等
重新构建速度 快速
不会产生文件,集成在打包后的文件中,不会产生列
function Hello(){
return <h1>hello world</h1>
}
这个是大家平时经常所写的jsx格式,jsx在打包编译的时候会被babel中的react插件转化成这样
function Hello() {
return React.createElement("h1", null, "hello world");
}
里面有一段是 React.createElement("h1", null, "hello world");
那么在React.createElement过程中到底发什么了什么呢?
/*
首先传入三种类型的参数
type:当前元素
config:当前元素属性
children:当前元素的子元素(包含嵌套元素)
*/
function createElement(type, config, children) {
/*
首先先定义一个props对象,里面存放着config里面的各种属性
例如 className,style之类
*/
let props = {};
for (let propsName in config) {
props[propsName] = config[propsName];
}
/*
在嵌套多个元素的时候就会传入多个children
除掉前面的type,config,剩下的length就是嵌套元素的数量
根据是单一元素还是多个元素判断要不要放在数组中
*/
const childrenLength = arguments.length - 2;
if (childrenLength === 1) {
props.children = children;
} else if (childrenLength > 1) {
let childrenArray = Array(childrenLength);
for (let i = 0; i < childrenLength; i++) {
childrenArray[i] = arguments[i + 2];
}
props.children = childrenArray;
}
/*
最后将封装好的props,以及type传入到另一个函数,reactElement当中
type: 'h1',
props: {
className: 'title',
style: {
fontSize: '14px'
},
children: 'hello, world'
}
*/
return reactElement(type, props);
}
然后我们再进到 reactElement函数中看看发生了什么
function reactElement(type, props) {
const element = {
type,
props
};
return element;
}
是的,这里就只是传进来的参数封装到一个对象里面然后抛出
而这个对象就是我们要寻找的 virtual dom
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.