You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We use React Context in B5X to hold onto T and its localized messages. Context objects themselves have to be created outside the React component tree, but if they're used with useContext and haven't been mounted in the component tree as a Provider, the caller will only get the initial value passed to createContext. So a common pattern is to pass createContext functions that throw errors so that the caller quickly realizes they're missing a crucial step. Another option is passing something like undefined, but then every caller has to check for that every time.
This bit us recently in B5X where there were two locale providers and we tried to useContext with the wrong one. Because we called createContext with makeT() or equivalent, we had a working T, but that made it a lot harder to realize what the problem was, since it never got its messages loaded and would fall back to basic string interpolation.
What I'd like to do is have something like const LocaleContext = createContext(makeErrorT("LocaleProvider does not exist in your component tree.")) so that any use of T(), T.$(), etc. errors with that message. I could create these functions myself, but that feels like a little too much coupling to t-i18n internals, and they seem generally useful for other projects too.
The text was updated successfully, but these errors were encountered:
We use React Context in B5X to hold onto
T
and its localized messages. Context objects themselves have to be created outside the React component tree, but if they're used withuseContext
and haven't been mounted in the component tree as a Provider, the caller will only get the initial value passed tocreateContext
. So a common pattern is to passcreateContext
functions that throw errors so that the caller quickly realizes they're missing a crucial step. Another option is passing something likeundefined
, but then every caller has to check for that every time.This bit us recently in B5X where there were two locale providers and we tried to
useContext
with the wrong one. Because we calledcreateContext
withmakeT()
or equivalent, we had a workingT
, but that made it a lot harder to realize what the problem was, since it never got its messages loaded and would fall back to basic string interpolation.What I'd like to do is have something like
const LocaleContext = createContext(makeErrorT("LocaleProvider does not exist in your component tree."))
so that any use ofT()
,T.$()
, etc. errors with that message. I could create these functions myself, but that feels like a little too much coupling to t-i18n internals, and they seem generally useful for other projects too.The text was updated successfully, but these errors were encountered: