diff --git a/.size-limit.js b/.size-limit.js index e1dc9a73..5a4e669e 100644 --- a/.size-limit.js +++ b/.size-limit.js @@ -38,7 +38,7 @@ module.exports = [ { name: 'adapter-react', path: 'react/index.min.mjs', - limit: '1618 b', + limit: '1604 b', ignore: ['react'], }, { diff --git a/CHANGELOG.md b/CHANGELOG.md index 10a68b82..cba336c0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,11 @@ # Version 5 +## 5.24.4 (2023-07-10) + +Fix: + - avoid recomputing react context [#684](https://github.com/ivanhofer/typesafe-i18n/issues/684) + ## 5.24.3 (2023-03-26) Fix: diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 2c13418f..1db1d252 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -4,7 +4,7 @@ Contributions are welcome. New ideas, improvements and optimizations are collect This project should keep following values: - - lightweight and performat i18n solution + - lightweight and performant i18n solution - best possible TypeScript support - easy to use (DX) - should work well together with other tools and services diff --git a/packages/adapter-react/src/index.tsx b/packages/adapter-react/src/index.tsx index 942b22c2..6a7f0963 100644 --- a/packages/adapter-react/src/index.tsx +++ b/packages/adapter-react/src/index.tsx @@ -50,17 +50,18 @@ export const initI18nReact = < const context = React.createContext({} as I18nContextType) const component: React.FunctionComponent> = (props) => { - const [locale, _setLocale] = React.useState(null as unknown as L) - const [LL, setLL] = React.useState(getFallbackProxy()) + const [locale, setLocale] = React.useState(props.locale) - const setLocale = (newLocale: L): void => { - _setLocale(newLocale) - setLL(() => i18nObject(newLocale, translations[newLocale], formatters[newLocale])) - } - - !locale && setLocale(props.locale) - - const ctx = { setLocale, locale, LL } as I18nContextType + const ctx = React.useMemo>( + () => ({ + setLocale, + locale, + LL: !locale + ? getFallbackProxy() + : i18nObject(locale, translations[locale], formatters[locale]), + }), + [setLocale, locale, translations, formatters], + ) return {props.children} } diff --git a/packages/adapter-solid/src/index.mts b/packages/adapter-solid/src/index.mts index 8d2f3d3a..f6f6d2c0 100644 --- a/packages/adapter-solid/src/index.mts +++ b/packages/adapter-solid/src/index.mts @@ -1,4 +1,13 @@ -import { batch, createComponent, createContext, createSignal, useContext, type Accessor, type Component, type JSX } from 'solid-js' +import { + batch, + createComponent, + createContext, + createSignal, + useContext, + type Accessor, + type Component, + type JSX, +} from 'solid-js' import { getFallbackProxy } from '../../runtime/src/core-utils.mjs' import type { BaseFormatters, BaseTranslation, Locale, TranslationFunctions } from '../../runtime/src/core.mjs' import { i18nObject } from '../../runtime/src/util.object.mjs' diff --git a/packages/runtime/src/index.mts b/packages/runtime/src/index.mts index 72059b6b..78504a15 100644 --- a/packages/runtime/src/index.mts +++ b/packages/runtime/src/index.mts @@ -1,5 +1,10 @@ -export { type BaseTranslation, type FormattersInitializer, type LocaleTranslations, type LocalizedString, type RequiredParams } from './core.mjs' +export { + type BaseTranslation, + type FormattersInitializer, + type LocaleTranslations, + type LocalizedString, + type RequiredParams, +} from './core.mjs' export { i18n, type LocaleTranslationFunctions } from './util.instance.mjs' export { i18nObject, typesafeI18nObject } from './util.object.mjs' export { i18nString, typesafeI18nString, type TranslateByString } from './util.string.mjs' -