React Dev Tools setInProps

时间:2017-10-27 16:55:50

标签: javascript reactjs ecmascript-6 react-props

我一直在查看GitHub repo的React Dev Tools,并将一些代码用于我自己的项目。我无法理解setInProps方法中path应该是什么:

function setInProps(internalInst, forceUpdate, path: Array<string | number>, value: any) {
  var element = internalInst._currentElement;
  internalInst._currentElement = {
    ...element,
    props: copyWithSet(element.props, path, value),
  };
  forceUpdate.call(internalInst._instance);
}

好像是某种类型的数组。它已传递给copyWithSet(...)

function copyWithSet(obj: Object | Array<any>, path: Array<string | number>, value: any): Object | Array<any> {
  return copyWithSetImpl(obj, path, 0, value);
}

然后copyWithSetImpl(...)

function copyWithSetImpl(obj, path, idx, value) {
  if (idx >= path.length) {
    return value;
  }
  var key = path[idx];
  var updated = Array.isArray(obj) ? obj.slice() : {...obj};
  // $FlowFixMe number or string is fine here
  updated[key] = copyWithSetImpl(obj[key], path, idx + 1, value);
  return updated;
}

有人可以举例说明如何使用setInProps更新道具中的内容吗?我已经有了内部实例对象,并假设value只是prop值。

0 个答案:

没有答案