Skip to content

Commit

Permalink
Merge pull request #63 from saitonakamura/improve-error-message
Browse files Browse the repository at this point in the history
Fixed uninitialized flex warning
  • Loading branch information
giulioz authored Sep 1, 2021
2 parents aca8e7d + 21fad24 commit 8d3be3a
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -21,6 +22,7 @@ const initialSharedFlexContext: SharedFlexContext = {
unregisterBox() {
console.warn('Flex not initialized! Please report')
},
notInitialized: true,
}

export const flexContext = createContext<SharedFlexContext>(initialSharedFlexContext)
Expand All @@ -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<SharedBoxContext>(initialSharedBoxContext)
4 changes: 2 additions & 2 deletions src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(context: React.Context<T>) {
export function useContext<T extends { notInitialized?: boolean }>(context: React.Context<T>) {
let result = useContextImpl(context)
if (!result) {
if (result.notInitialized) {
console.warn('You must place this hook/component under a <Flex/> component!')
}
return result
Expand Down

0 comments on commit 8d3be3a

Please sign in to comment.