@samitouri / QOS-React / commits / bae18b4dfe

[compiler] Add flag for lowering context access

*This is only for internal profiling, not intended to ship.* ghstack-source-id: e48998b7be4272199c8a6ff9cc2ec0975add5030 Pull Request resolved: https://github.com/facebook/react/pull/30547

Sathya Gunsasekaran committed Jul 31, 2024 at 14:35 UTC bae18b4dfe122bb9ccc3e3553ecdc1af654cd217
1 file changed +19
compiler/packages/babel-plugin-react-compiler/src/HIR/Environment.ts
+19
@@ -434,6 +434,25 @@ const EnvironmentConfigSchema = z.object({
434 * Here the variables `ref` and `myRef` will be typed as Refs.
435 */
436 enableTreatRefLikeIdentifiersAsRefs: z.boolean().nullable().default(false),
437 +
438 + /*
439 + * If enabled, this lowers any calls to `useContext` hook to use a selector
440 + * function.
441 + *
442 + * The compiler automatically figures out the keys by looking for the immediate
443 + * destructuring of the return value from the useContext call. In the future,
444 + * this can be extended to different kinds of context access like property
445 + * loads and accesses over multiple statements as well.
446 + *
447 + * ```
448 + * // input
449 + * const {foo, bar} = useContext(MyContext);
450 + *
451 + * // output
452 + * const {foo, bar} = useContext(MyContext, (c) => [c.foo, c.bar]);
453 + * ```
454 + */
455 + enableLowerContextAccess: z.boolean().nullable().default(false),
456 });
457
458 export type EnvironmentConfig = z.infer<typeof EnvironmentConfigSchema>;