Replies: 3 comments 4 replies
-
context in react does not pierce reconcilers, take a look: #262 you can either solve it by forwarding context, but i recommend zustand: https://github.com/react-spring/zustand |
Beta Was this translation helpful? Give feedback.
-
Instead of using useContext, I have restructure the app to a single React Class Component. I make all subcomponents and functions as property in the main class component, so states are shared amongst. Thanks for the help! |
Beta Was this translation helpful? Give feedback.
-
Forwarding context is actually pretty easy. I ended up crafting this small bridge component as a helper utility. import React, {FC, useMemo} from 'react';
const ContextBridge = ({contexts = [], children}) => {
const reversed = useMemo(() => contexts.reverse(), [contexts]);
return reversed.reduce(
(acc, {context: Context, value}) => (
<Context.Provider value={value}>{acc}</Context.Provider>
),
children
);
};
export default ContextBridge; Usage: const MyApp = () => {
const ContextValue1 = useContext(Context1);
const ContextValue2 = useContext(Context2);
return (
<Canvas>
<ContextBridge
contexts={[
{context: Context1, value: ContextValue1},
{context: Context2, value: ContextValue2},
]}
>
{/* R3F content goes here */}
</ContextBridge>
</Canvas>
)
}
export default MyApp; Basically, it just recursively nests each provider in the order that it is given. It was definitely a fun use of the |
Beta Was this translation helpful? Give feedback.
-
Hi all,
I am working on a project to make use of
PointerEvent
in canvas.tsxI was using useContext Hooks but realized it was impossible to include
PointPrinter
andApp
together as children inProvider
methodCode summary :
Anyone came across the same difficulties when trying to access event states with another pure React component? Any explanation is highly appreciated in advance! Thank you.
Beta Was this translation helpful? Give feedback.
All reactions