Skip to content

Commit

Permalink
Inject overrideProps() fn to DevTools (#14427)
Browse files Browse the repository at this point in the history
* Inject overrideProps() fn to DevTools

This function will enable editing props for function components, host nodes, and special types like memo and forwardRef.
  • Loading branch information
bvaughn committed Dec 13, 2018
1 parent a22880e commit 7325ebe
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions packages/react-reconciler/src/ReactFiberReconciler.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ import {
current as ReactCurrentFiberCurrent,
} from './ReactCurrentFiber';
import {StrictMode} from './ReactTypeOfMode';
import {Sync} from './ReactFiberExpirationTime';

type OpaqueRoot = FiberRoot;

Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 7325ebe

Please sign in to comment.