From 21fad244df241bd2e477d0a485f5d95c9ffed2fc Mon Sep 17 00:00:00 2001 From: Michael Bashurov Date: Fri, 27 Aug 2021 11:07:35 +0300 Subject: [PATCH] Fixed uninitialized flex warning Based on the discussion around https://discordapp.com/channels/740090768164651008/745777196911689748/880301261231099944 Also turned out warning is already here, but it doesn't work, so I fixed it Feels a bit hacky to me, but I think it's good enough --- src/context.ts | 4 ++++ src/hooks.ts | 4 ++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/context.ts b/src/context.ts index 04ed578..03c242d 100644 --- a/src/context.ts +++ b/src/context.ts @@ -8,6 +8,7 @@ export interface SharedFlexContext { requestReflow(): void registerBox(node: YogaNode, group: Group, flexProps: R3FlexProps, centerAnchor?: boolean): void unregisterBox(node: YogaNode): void + notInitialized?: boolean } const initialSharedFlexContext: SharedFlexContext = { @@ -21,6 +22,7 @@ const initialSharedFlexContext: SharedFlexContext = { unregisterBox() { console.warn('Flex not initialized! Please report') }, + notInitialized: true, } export const flexContext = createContext(initialSharedFlexContext) @@ -29,11 +31,13 @@ export interface SharedBoxContext { node: YogaNode | null size: [number, number] centerAnchor?: boolean + notInitialized?: boolean } const initialSharedBoxContext: SharedBoxContext = { node: null, size: [0, 0], + notInitialized: true, } export const boxContext = createContext(initialSharedBoxContext) diff --git a/src/hooks.ts b/src/hooks.ts index 17ded20..79ae372 100644 --- a/src/hooks.ts +++ b/src/hooks.ts @@ -2,9 +2,9 @@ import { useCallback, useContext as useContextImpl, useMemo } from 'react' import { Mesh, Vector3 } from 'three' import { flexContext, boxContext } from './context' -export function useContext(context: React.Context) { +export function useContext(context: React.Context) { let result = useContextImpl(context) - if (!result) { + if (result.notInitialized) { console.warn('You must place this hook/component under a component!') } return result