Skip to content

Commit

Permalink
Fixed uninitialized flex warning
Browse files Browse the repository at this point in the history
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
  • Loading branch information
saitonakamura committed Aug 30, 2021
1 parent ab622a2 commit 21fad24
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 21fad24

Please sign in to comment.