Giter Club home page Giter Club logo

blog's Introduction

记录一下每天学习的笔记或者工作中的问题

React virtual dom

blog's People

Contributors

qiantaobao avatar

Watchers

James Cloos avatar  avatar

blog's Issues

webpack

webpack 笔记

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

构建速度 慢
重新构建速度 慢
源码映射,会单独产生一个单独的source map文件,出错会显示当前的行和列,是一个比较大和全的错误调试配置

  • eval-source-map

构建速度 慢
重新构建速度 比较快
不会产生单独的文件,但会显示行和列

  • cheap-module-source-map

构建速度 中等
重新构建速度 比较慢
不会产生列,但是会产生一个单独的映射文件

  • cheap-module-eval-source-map

构建速度 中等
重新构建速度 快速
不会产生文件,集成在打包后的文件中,不会产生列

谈谈React中的virtual dom

在面试中经常会被问到什么virtual dom,虽然从网上了解到是一个js对象,但到底是什么,怎么工作的,还是一知半解,下面我们通过简易版的React源码来看看他的真面目。

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.