I have a React component that I'm injecting some MobX values into. It contains a deeply-nested tree of children. Rather than injecting all the values that all the children need into the parent component, and passing them down through the whole tree, I also inject some data directly into individual child components.
This works fine, but what if I want two instances of this tree in my app, using different data? I can inject different data into the two instances of the top component, but how do I differentiate the child components that also need different data injected?
For example, if I use component A in two places, I can inject two different values for x into them. But if A contains component C as a descendant, how can I inject two different values for y into them (other than injecting them into each A and passing them down the tree)?
------ root --------
/ \
A <-- inject foo.x A <-- inject bar.x
/ \
B B
/ \
C <-- inject foo.y C <-- inject bar.y
I'm using the "Customizing inject" method of injecting, so my components take individual values rather than a single store.
const UserNameDisplayer = inject(
stores => ({
name: stores.userStore.name
})
)(NameDisplayer)
Not sure whether that's the best way to do it though.