Losing React Context in <Canvas/> children #1122
-
It seems that react contexts cannot be passed through the Sample code: const ColorContext = createContext('???')
const LosingColor = () => {
const color = useContext(ColorContext)
console.log(color) // color is '???' (the default value)
return null
}
const UsingColor = () => {
const color = useContext(ColorContext)
console.log(color) // color is 'red' (ok)
return null
}
ReactDOM.render(
<ColorContext.Provider value="red">
<Canvas>
<LosingColor />
</Canvas>
<UsingColor />
</ColorContext.Provider>,
document.getElementById('root')
) Is there any solution? Motivation: |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
I think this is written in the documentation as a caveat. You should check out ‘drei’s contextBridge component |
Beta Was this translation helpful? Give feedback.
-
this is a react issue, <Canvas context={[ColorContext]}>
<LosingColor />
</Canvas> EDIT (josh): we're not supporting context passing this way. |
Beta Was this translation helpful? Give feedback.
I think this is written in the documentation as a caveat. You should check out ‘drei’s contextBridge component
EDIT: https://github.com/pmndrs/drei#usecontextbridge