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
As far as I can tell there's currently no simple and convenient way with React-Intl of supporting instant locale switching in a large app that uses pure components (such as your standard usage of react-redux).
I think the only good solution to this problem is the observer pattern, which would entail something along the lines of the following:
the intl object created by IntlProvider and shared on the context includes a subscribe method
consuming components like FormattedMessage or InjectIntl call context.intl.subscribe on componentWillMount with a listener and receive back a unsubscribe method
upon receiving a new value for locale, IntlProvider notifies all listeners
the listener calls forceUpdate on the consuming component, thus directly triggering the required updates
the unsubscribe function returned by the intl.subscribe is stored on the consuming component then called in componentWillUnmount. It removes the corresponding listener from the collection maintained on IntlProvider.
I believe the result would be a clean abstraction (no extra configuration or usage gotchas necessary for users), that should solve the problem seamlessly with minimal performance implications. However if there were any concerns about enabling this by default it could be enabled via a prop on IntlProvider, after-all not everyone uses pure components and needs instant locale switching.
I'd be happy to put together a pull request if you think this is a good idea and likely to be merged.
The text was updated successfully, but these errors were encountered:
nat-n
changed the title
Enable elegant instant locale switching by making context consumers subscribe to IntlProvider
Support instant locale switching by making context consumers subscribe to IntlProvider
Jul 6, 2017
I was trying to find an existing solution but I failed. So I decided to build my own on top of React-intl and new React Context syntax.
You can find the results here: https://stackblitz.com/edit/react-locales
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
As far as I can tell there's currently no simple and convenient way with React-Intl of supporting instant locale switching in a large app that uses pure components (such as your standard usage of react-redux).
I think the only good solution to this problem is the observer pattern, which would entail something along the lines of the following:
I believe the result would be a clean abstraction (no extra configuration or usage gotchas necessary for users), that should solve the problem seamlessly with minimal performance implications. However if there were any concerns about enabling this by default it could be enabled via a prop on IntlProvider, after-all not everyone uses pure components and needs instant locale switching.
I'd be happy to put together a pull request if you think this is a good idea and likely to be merged.
The text was updated successfully, but these errors were encountered: