context
Hi all, I have a question about context scoping.
In the example above, I have three main components and a HOC which wraps a component in a context.
C
renders A
which contains 2 B
s as children. A
also has a provider wrapped around it.
Within all three components, I try to retrieve a context called FooContext
which is provided by the aforementioned HOC.
As expected I was not able to retrieve the context in C
but I was initially surprised that I am not able to retrieve the context in the first B
but I am able to in the second B
. I assume that this is because it lazily initialized so it is put in the "expected" place in the owner tree.
First I just wanted to check if this is expected behaviour.
Secondly is there anyway to catch these kinds of subtle issues in the future e.g. in a lint rule?3 Replies
I am not able to retrieve the context in the first B but I am able to in the second BWith
b1
you are forcing JSX evaluation at setup time; so it's in exactly the same position as the console.log()
. b2
is a function, so its evaluation is deferred until it is inside the returned JSX where the provider is active.Yep makes sense. This seems to me like a really subtle difference with big implications. I can imagine myself and others forcing the evaluation at setup time but still thinking that they will be able to retrieve the context because they are still under
A
in the component tree.
Do you think it would be reasonable to use a lint rule to disable these eager evaluations of components during setup to avoid this?There was a whole discussion about this some time ago:
https://discord.com/channels/722131463138705510/1238643925988937820
The thing is, sometimes you need early evaluation that is why children exists.