Inject overrideProps() fn to DevTools (#14427)

* Inject overrideProps() fn to DevTools

This function will enable editing props for function components, host nodes, and special types like memo and forwardRef.
This commit is contained in:
Brian Vaughn authored and GitHub committed 2018-12-13 09:40:59 -08:00
1 parent a22880e5e5
commit 7325ebe4d6
1 file changed
+40
+40
View File
@@ -62,6 +62,7 @@ import {
current as ReactCurrentFiberCurrent,
} from './ReactCurrentFiber';
import {StrictMode} from './ReactTypeOfMode';
import {Sync} from './ReactFiberExpirationTime';
type OpaqueRoot = FiberRoot;
@@ -339,10 +340,49 @@ export function findHostInstanceWithNoPortals(
return hostFiber.stateNode;
}
let overrideProps = null;
if (__DEV__) {
const copyWithSetImpl = (
obj: Object | Array<any>,
path: Array<string | number>,
idx: number,
value: any,
) => {
if (idx >= path.length) {
return value;
}
const key = path[idx];
const 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;
};
const copyWithSet = (
obj: Object | Array<any>,
path: Array<string | number>,
value: any,
): Object | Array<any> => {
return copyWithSetImpl(obj, path, 0, value);
};
// Support DevTools props for function components, forwardRef, memo, host components, etc.
overrideProps = (fiber: Fiber, path: Array<string | number>, value: any) => {
flushPassiveEffects();
fiber.pendingProps = copyWithSet(fiber.memoizedProps, path, value);
if (fiber.alternate) {
fiber.alternate.pendingProps = fiber.pendingProps;
}
scheduleWork(fiber, Sync);
};
}
export function injectIntoDevTools(devToolsConfig: DevToolsConfig): boolean {
const {findFiberByHostInstance} = devToolsConfig;
return injectInternals({
...devToolsConfig,
overrideProps,
findHostInstanceByFiber(fiber: Fiber): Instance | TextInstance | null {
const hostFiber = findCurrentHostFiber(fiber);
if (hostFiber === null) {