2

我一直在查看 React Dev Tools 的GitHub 存储库,并将他们的一些代码用于我自己的项目。我无法理解方法path中应该包含的内容setInProps

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只是道具值。

4

0 回答 0