Conditional SSR context, unified interface?
Unanswered
Devon Rex posted this in #help-forum
Devon RexOP
Is it possible in NextJS to conditional render either context A or B from the server, and have hooks that dispatch the correct logic underneath such that hook set C adapts to either underlying context?
3 Replies
Black carp
No. If you dynamically render a context provider, executing a hook that depends on it will result in an exception being thrown.
That being said, you could have fallback values for contexts A and B, so that you know if either was defined (A.k.a. their value is on the fallback one). Then in your hooks you could check under which scenario you currently are and act accordingly.
That being said, you could have fallback values for contexts A and B, so that you know if either was defined (A.k.a. their value is on the fallback one). Then in your hooks you could check under which scenario you currently are and act accordingly.
@Black carp No. If you dynamically render a context provider, executing a hook that depends on it will result in an exception being thrown.
That being said, you could have fallback values for contexts A and B, so that you know if either was defined (A.k.a. their value is on the fallback one). Then in your hooks you could check under which scenario you currently are and act accordingly.
Devon RexOP
This is what I ended up doing. Setting undefined to every context value let me do a simple check to see which context is in use, and then the right stuff is dispatched accordingly
Thanks for the answer!