Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

284
Views
Unable to use React Context in deep descendant components

I am trying to use global state in my React app using React Context and the useReducer hook. When I try to access state using my useStateContext hook in distant descendants, I get back undefined. To preface, I am using react-three-fiber where I am getting my context error.

App.js

const App = () => {
  return (
      <GameProvider>
        <AppState />
      </GameProvider>
  );
};

const AppState = () => {
  const state = useStateContext() // this works. Direct child of Provider
  return <Game />
}

const Game = () => {
  const state = useStateContext(); // this works. 2nd descendant of Provider
  return <Scene/>
};

const Scene = () => {
  const state = useStateContext(); // this works. 3rd descendant
  return (
    <div id={styles.scene}>
      <Canvas >
        <OrbitControls minDistance={9} maxDistance={9} />
        <ambientLight intensity={0.5} />
        <spotLight position={[10, 15, 10]} angle={0.3} />
        <Cube
          size={5}
          position={[0, 0, 0]}
        />
      </Canvas>
    </div>
  );
};

const Cube = () => {
  const state = useStateContext(); // returns undefined. What gives?
  return <group></group>
}

StateContext.js

import React, { useContext, createContext, useReducer } from 'react'
import reducer from './reducer';

const StateContext = createContext();
const DispatchContext = createContext();

export const useDispatchContext = () => useContext(DispatchContext)
export const useStateContext = () => useContext(StateContext);

const initialState = {
     test: 1
}

export const GameProvider = ({ children }) => {
    const [state, dispatch] = useReducer(reducer, initialState)

    return (
        <DispatchContext.Provider value={dispatch}>
            <StateContext.Provider
                value={state}
            >
                {children}
            </StateContext.Provider>
        </DispatchContext.Provider>
    )
}

I wanted to use Context since I figured I could get the state in any descendant of my application. I've tried checking the react docs about this issue but to no avail.

EDIT:

Also I think it might be important to mention that I am using react-three-fiber which is wrapping my Cube component in a Canvas.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Turns out this is a pretty common issue with using the React Context api with react-three-fiber. Through the documentation I decided to use react-three-drei's useContextBridge since I was already using the package. Found the documentation here for my solution

const ContextBridge = useContextBridge(StateContext, DispatchContext)
  return (
    <div id={styles.scene}>
      <Canvas >
        <OrbitControls minDistance={9} maxDistance={9} />
        <ambientLight intensity={0.5} />
        <spotLight position={[10, 15, 10]} angle={0.3} />
        <spotLight position={[10, -15, -30]} angle={0.3} />
        <ContextBridge>
          <Cube
            size={5}
            position={[0, 0, 0]}
          />
        </ContextBridge>
      </Canvas>
    </div>
  );
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!