Skip to content

Commit 2069c6c

Browse files
authored
fix: avoid recomputing react context (#684) (#685)
1 parent 7210b63 commit 2069c6c

File tree

6 files changed

+35
-15
lines changed

6 files changed

+35
-15
lines changed

.size-limit.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ module.exports = [
3838
{
3939
name: 'adapter-react',
4040
path: 'react/index.min.mjs',
41-
limit: '1618 b',
41+
limit: '1604 b',
4242
ignore: ['react'],
4343
},
4444
{

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
# Version 5
44

5+
## 5.24.4 (2023-07-10)
6+
7+
Fix:
8+
- avoid recomputing react context [#684](https://github.com/ivanhofer/typesafe-i18n/issues/684)
9+
510
## 5.24.3 (2023-03-26)
611

712
Fix:

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Contributions are welcome. New ideas, improvements and optimizations are collect
44

55
This project should keep following values:
66

7-
- lightweight and performat i18n solution
7+
- lightweight and performant i18n solution
88
- best possible TypeScript support
99
- easy to use (DX)
1010
- should work well together with other tools and services

packages/adapter-react/src/index.tsx

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,18 @@ export const initI18nReact = <
5050
const context = React.createContext({} as I18nContextType<L, T, TF>)
5151

5252
const component: React.FunctionComponent<TypesafeI18nProps<L>> = (props) => {
53-
const [locale, _setLocale] = React.useState<L>(null as unknown as L)
54-
const [LL, setLL] = React.useState<TF>(getFallbackProxy<TF>())
53+
const [locale, setLocale] = React.useState<L>(props.locale)
5554

56-
const setLocale = (newLocale: L): void => {
57-
_setLocale(newLocale)
58-
setLL(() => i18nObject<L, T, TF, F>(newLocale, translations[newLocale], formatters[newLocale]))
59-
}
60-
61-
!locale && setLocale(props.locale)
62-
63-
const ctx = { setLocale, locale, LL } as I18nContextType<L, T, TF>
55+
const ctx = React.useMemo<I18nContextType<L, T, TF>>(
56+
() => ({
57+
setLocale,
58+
locale,
59+
LL: !locale
60+
? getFallbackProxy<TF>()
61+
: i18nObject<L, T, TF, F>(locale, translations[locale], formatters[locale]),
62+
}),
63+
[setLocale, locale, translations, formatters],
64+
)
6465

6566
return <context.Provider value={ctx}>{props.children}</context.Provider>
6667
}

packages/adapter-solid/src/index.mts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1-
import { batch, createComponent, createContext, createSignal, useContext, type Accessor, type Component, type JSX } from 'solid-js'
1+
import {
2+
batch,
3+
createComponent,
4+
createContext,
5+
createSignal,
6+
useContext,
7+
type Accessor,
8+
type Component,
9+
type JSX,
10+
} from 'solid-js'
211
import { getFallbackProxy } from '../../runtime/src/core-utils.mjs'
312
import type { BaseFormatters, BaseTranslation, Locale, TranslationFunctions } from '../../runtime/src/core.mjs'
413
import { i18nObject } from '../../runtime/src/util.object.mjs'

packages/runtime/src/index.mts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1-
export { type BaseTranslation, type FormattersInitializer, type LocaleTranslations, type LocalizedString, type RequiredParams } from './core.mjs'
1+
export {
2+
type BaseTranslation,
3+
type FormattersInitializer,
4+
type LocaleTranslations,
5+
type LocalizedString,
6+
type RequiredParams,
7+
} from './core.mjs'
28
export { i18n, type LocaleTranslationFunctions } from './util.instance.mjs'
39
export { i18nObject, typesafeI18nObject } from './util.object.mjs'
410
export { i18nString, typesafeI18nString, type TranslateByString } from './util.string.mjs'
5-

0 commit comments

Comments
 (0)