I'm adding React to an existing webapp. For now, I'm selectively replacing parts of the page, rendering different components in different divs. For this reason I don't have a single tree from where all components hang. I would like to use one context provider to share context information across all these components, but since I don't have a single tree I can't make them all hang from the same context provider.
Is there a way to use the default context defined like this?
const MyContext = React.createContext(some_data);
and to have NO provider from which components hang, rather only consumers?
<MyContext.Consumer>...</MyContext.Consumer>
It works for the default value, but then I don't know how to change the value of this default context.
Is my understanding correct and this is meant for all consumers hanging from a provider? or is there a way to programatically set the value of the default context? Is there another way to approch this?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…