实现setState(监听state, 调用rerender方法)
main.js
import {render, Component, createElement} from './toy-react.js'// window.a = <div id="app" class="head"></div>class Hello extends Component {constructor () {super()this.state = {count: 0,name: 'Jack'}}render () {return <div><button onClick={() => {this.setState({count: this.state.count+1})} }>add</button><p>count:{this.state.count.toString()}</p><p>name:{this.state.name}</p>{this.children}</div>}}window.a = <Hello id="app" class="head"><span>1</span><span>2</span><span><h1>3</h1><h1>4</h1></span>hello</Hello>render(window.a, document.body)
toy-react.js
const RENDER_TO_DOM = Symbol('render to dom')export class Component {constructor() {this.props = Object.create(null);this.children = [];this._root = null;this._range = null;}setAttribute(name, value) {this.props[name] = value}appendChild(component) {this.children.push(component)}[RENDER_TO_DOM](range) {this._range = rangethis.render()[RENDER_TO_DOM](range)}rerender() {this._range.deleteContents()this[RENDER_TO_DOM](this._range)}setState (newState) {// console.log(newState)// 没有state 或者 不是对象, 短路if (this.state == null || typeof this.state != "object") {this.setState = newStatethis.rerender()return;}// 深拷贝let merge = (oldState, newState) => {for (let p in newState) {if (oldState[p] == null || typeof oldState[p] != "object") {oldState[p] = newState[p]} else {merge(oldState[p], newState[p])}}}merge(this.state, newState)this.rerender()}// get root() {// if (!this._root) {// this._root = this.render().root// }// return this._root// }}class ElementWrapper {constructor(type) {this.root = document.createElement(type)}setAttribute(name, value) {// 过滤事件, 如onClickif (name.match(/^on([\s\S]+)$/)) {// console.log(RegExp.$1)// 绑定事件, Click转clickthis.root.addEventListener(RegExp.$1.replace(/^[\s\S]/, c => c.toLowerCase()), value)} else {this.root.setAttribute(name, value)}this.root.setAttribute(name, value)}appendChild(component) {// this.root.appendChild(component.root)let range = document.createRange()range.setStart(this.root, this.root.childNodes.length)range.setEnd(this.root, this.root.childNodes.length)component[RENDER_TO_DOM](range)}[RENDER_TO_DOM](range){range.deleteContents()range.insertNode(this.root)}}class TextWrapper {constructor(content) {this.root = document.createTextNode(content)}[RENDER_TO_DOM](range){range.deleteContents()range.insertNode(this.root)}}export function createElement(tagName, attributes, ...rest) {let elementif (typeof tagName == 'string') {element = new ElementWrapper(tagName)} else {element = new tagName}if (typeof attributes === 'object' && attributes instanceof Object) {for (const key in attributes) {element.setAttribute(key, attributes[key])}}let insertChildren = (children) => {// console.log(children)for(const child of children) {if (typeof child == 'string') {child = new TextWrapper(child)}if ((typeof child == 'object') && (child instanceof Array)) {insertChildren(child)} else {element.appendChild(child)}}}insertChildren(rest)return element}export function render(component, parentElement) {// parentElement.appendChild(component.root)let range = document.createRange()range.setStart(parentElement, 0)range.setEnd(parentElement, parentElement.childNodes.length)component[RENDER_TO_DOM](range)}
