Clean up process globals

Summary: Cleaned up some `process` references

Reviewed By: passy

Differential Revision: D33184882

fbshipit-source-id: 6fc56bcea4e145ba97aa8bf9e689e53e2bf239cc
This commit is contained in:
Michel Weststrate
2021-12-17 14:11:20 -08:00
committed by Facebook GitHub Bot
parent e46fcba0b2
commit 4630f37786
6 changed files with 36 additions and 25 deletions

View File

@@ -7,27 +7,23 @@
* @format
*/
import {isProduction} from 'flipper-common';
import {useRef} from 'react';
// TODO: create isProduction utility!
declare const process: any;
/**
* This hook will throw in development builds if the value passed in is unstable.
* Use this if to make sure consumers aren't creating or changing certain props over time
* (intentionally or accidentally)
*/
export const useAssertStableRef =
typeof process !== 'undefined' &&
(process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'test')
? function useAssertStableRef(value: any, prop: string) {
const ref = useRef(value);
if (ref.current !== value) {
throw new Error(
`[useAssertStableRef] An unstable reference was passed to this component as property '${prop}'. For optimization purposes we expect that this prop doesn't change over time. You might want to create the value passed to this prop outside the render closure, store it in useCallback / useMemo / useState, or set a key on the parent component`,
);
}
export const useAssertStableRef = !isProduction()
? function useAssertStableRef(value: any, prop: string) {
const ref = useRef(value);
if (ref.current !== value) {
throw new Error(
`[useAssertStableRef] An unstable reference was passed to this component as property '${prop}'. For optimization purposes we expect that this prop doesn't change over time. You might want to create the value passed to this prop outside the render closure, store it in useCallback / useMemo / useState, or set a key on the parent component`,
);
}
: function (_value: any, _prop: string) {
// no-op
};
}
: function (_value: any, _prop: string) {
// no-op
};