Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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