Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

9.2 #4

Closed
RubyLouvre opened this issue Apr 21, 2017 · 3 comments
Closed

9.2 #4

RubyLouvre opened this issue Apr 21, 2017 · 3 comments

Comments

@RubyLouvre
Copy link
Owner

diffProps 方法不能修改props属性,因为它可能已经冻结了

 var builtIdProperties = /^(?:className|id|title|htmlFor)$/
  export function diffProps(dom, instance, props, nextProps) {

      if (props === nextProps) {
          return
      }

      for (let name in nextProps) {
          if (name === 'children') {
              continue
          }
          var val = nextProps[name]
          if (name === 'ref') {
              if (props[name] !== val) {
                  instance && patchRef(instance, val, dom)
              }
              continue
          }
          if (name === 'style') {
              patchStyle(dom, props[style], val)
              continue
          }
          if (name === 'dangerouslySetInnerHTML') {
              var oldhtml = props[name] && props[name]._html
              instance && (instance._hasSetInnerHTML = true)
              if (val && val._html !== oldhtml) {
                  dom.innerHTML = val._html
              }
          }
          if (isEvent(name)) {
              if (!props[name]) { //添加全局监听事件
                  var eventName = getBrowserName(name)
                  addGlobalEventListener(eventName)
              }
              if (inMobile && eventName === 'click') {
                  elem.addEventListener('click', clickHack)

              }
              var events = (dom.__events || (dom.__events = {}))
                  //   events[name] = props[name] = val
              events[name] = val
              continue
          }

          if (val !== props[name]) {
              //移除属性
              if (val === false || val === void 666 || val === null) {
                  dom.removeAttribute(name)
                      // delete props[name]
              } else { //添加新属性
                  if (builtIdProperties.test(name)) {
                      dom[name] = val + ''
                  } else {
                      dom.setAttribute(name, val + '')
                  }

                  //  props[name] = val // 不能改旧的props
              }
          }
      }
      for (let name in props) {
          if (!(name in nextProps)) {
              if (isEvent(name)) { //移除事件
                  var events = dom.__events || {}
                  delete events[name]
              } else { //移除属性
                  if (builtIdProperties.test(name)) {
                      dom[name] = ''
                  } else {
                      dom.removeAttribute(name)
                  }
              }
              // delete props[name]
          }
      }
  }
@RubyLouvre
Copy link
Owner Author

function render(vnode, container, cb) {
    container.textContent = ''
    while (container.firstChild) {
        container.removeChild(container.firstChild)
    }
    var root = createElement(TopLevelWrapper, { child: vnode });
    transaction.isInTransation = true
    var root = toVnode(vnode, {})
    transaction.isInTransation = false
    root.instance.container = container
    root.instance.forceUpdate(cb)
    return root.instance || null
}

ReactDOM.render应该返回 根组件的实例

@RubyLouvre
Copy link
Owner Author

updateComponent方法中的this.forceUpdate应该改成instance.forceUpdate

@RubyLouvre
Copy link
Owner Author

RubyLouvre commented May 2, 2017

原patchRef方法理解有误,函数形式的传参是DOM 不是组件实例

function patchRef(instance, ref, dom, mount) {
      if (typeof ref === 'function') {
          ref(dom)
      } else if (typeof ref === 'string') {
          instance.refs[ref] = dom
          dom.getDOMNode = getDOMNode
      }
  }

BetaSu pushed a commit to BetaSu/anu that referenced this issue Nov 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant