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
が単なる prop 値であると想定しています。