@lingui/react 4.4.1 → 4.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -1,7 +1,6 @@
1
1
  'use strict';
2
2
 
3
3
  const React = require('react');
4
- const shim = require('use-sync-external-store/shim');
5
4
 
6
5
  const LinguiContext = React.createContext(null);
7
6
  function useLingui() {
@@ -18,6 +17,7 @@ const I18nProvider = ({
18
17
  defaultComponent,
19
18
  children
20
19
  }) => {
20
+ const latestKnownLocale = React.useRef(i18n.locale);
21
21
  const makeContext = React.useCallback(
22
22
  () => ({
23
23
  i18n,
@@ -26,36 +26,25 @@ const I18nProvider = ({
26
26
  }),
27
27
  [i18n, defaultComponent]
28
28
  );
29
- const context = React.useRef(makeContext());
30
- const subscribe = React.useCallback(
31
- (onStoreChange) => {
32
- const renderWithFreshContext = () => {
33
- context.current = makeContext();
34
- onStoreChange();
35
- };
36
- const propsChanged = context.current.i18n !== i18n || context.current.defaultComponent !== defaultComponent;
37
- if (propsChanged) {
38
- renderWithFreshContext();
39
- }
40
- return i18n.on("change", renderWithFreshContext);
41
- },
42
- [makeContext, i18n, defaultComponent]
43
- );
44
- const getSnapshot = React.useCallback(() => {
45
- return context.current;
46
- }, []);
47
- const contextObject = shim.useSyncExternalStore(
48
- subscribe,
49
- getSnapshot,
50
- getSnapshot
51
- );
52
- if (!contextObject.i18n.locale) {
29
+ const [context, setContext] = React.useState(makeContext());
30
+ React.useEffect(() => {
31
+ const updateContext = () => {
32
+ latestKnownLocale.current = i18n.locale;
33
+ setContext(makeContext());
34
+ };
35
+ const unsubscribe = i18n.on("change", updateContext);
36
+ if (latestKnownLocale.current !== i18n.locale) {
37
+ updateContext();
38
+ }
39
+ return unsubscribe;
40
+ }, [i18n, makeContext]);
41
+ if (!latestKnownLocale.current) {
53
42
  process.env.NODE_ENV === "development" && console.log(
54
43
  "I18nProvider rendered `null`. A call to `i18n.activate` needs to happen in order for translations to be activated and for the I18nProvider to render.This is not an error but an informational message logged only in development."
55
44
  );
56
45
  return null;
57
46
  }
58
- return /* @__PURE__ */ React.createElement(LinguiContext.Provider, { value: contextObject }, children);
47
+ return /* @__PURE__ */ React.createElement(LinguiContext.Provider, { value: context }, children);
59
48
  };
60
49
 
61
50
  const tagRe = /<([a-zA-Z0-9]+)>(.*?)<\/\1>|<([a-zA-Z0-9]+)\/>/;
package/dist/index.mjs CHANGED
@@ -1,5 +1,4 @@
1
- import React, { useCallback, useRef } from 'react';
2
- import { useSyncExternalStore } from 'use-sync-external-store/shim';
1
+ import React from 'react';
3
2
 
4
3
  const LinguiContext = React.createContext(null);
5
4
  function useLingui() {
@@ -16,7 +15,8 @@ const I18nProvider = ({
16
15
  defaultComponent,
17
16
  children
18
17
  }) => {
19
- const makeContext = useCallback(
18
+ const latestKnownLocale = React.useRef(i18n.locale);
19
+ const makeContext = React.useCallback(
20
20
  () => ({
21
21
  i18n,
22
22
  defaultComponent,
@@ -24,36 +24,25 @@ const I18nProvider = ({
24
24
  }),
25
25
  [i18n, defaultComponent]
26
26
  );
27
- const context = useRef(makeContext());
28
- const subscribe = useCallback(
29
- (onStoreChange) => {
30
- const renderWithFreshContext = () => {
31
- context.current = makeContext();
32
- onStoreChange();
33
- };
34
- const propsChanged = context.current.i18n !== i18n || context.current.defaultComponent !== defaultComponent;
35
- if (propsChanged) {
36
- renderWithFreshContext();
37
- }
38
- return i18n.on("change", renderWithFreshContext);
39
- },
40
- [makeContext, i18n, defaultComponent]
41
- );
42
- const getSnapshot = useCallback(() => {
43
- return context.current;
44
- }, []);
45
- const contextObject = useSyncExternalStore(
46
- subscribe,
47
- getSnapshot,
48
- getSnapshot
49
- );
50
- if (!contextObject.i18n.locale) {
27
+ const [context, setContext] = React.useState(makeContext());
28
+ React.useEffect(() => {
29
+ const updateContext = () => {
30
+ latestKnownLocale.current = i18n.locale;
31
+ setContext(makeContext());
32
+ };
33
+ const unsubscribe = i18n.on("change", updateContext);
34
+ if (latestKnownLocale.current !== i18n.locale) {
35
+ updateContext();
36
+ }
37
+ return unsubscribe;
38
+ }, [i18n, makeContext]);
39
+ if (!latestKnownLocale.current) {
51
40
  process.env.NODE_ENV === "development" && console.log(
52
41
  "I18nProvider rendered `null`. A call to `i18n.activate` needs to happen in order for translations to be activated and for the I18nProvider to render.This is not an error but an informational message logged only in development."
53
42
  );
54
43
  return null;
55
44
  }
56
- return /* @__PURE__ */ React.createElement(LinguiContext.Provider, { value: contextObject }, children);
45
+ return /* @__PURE__ */ React.createElement(LinguiContext.Provider, { value: context }, children);
57
46
  };
58
47
 
59
48
  const tagRe = /<([a-zA-Z0-9]+)>(.*?)<\/\1>|<([a-zA-Z0-9]+)\/>/;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lingui/react",
3
- "version": "4.4.1",
3
+ "version": "4.4.2",
4
4
  "sideEffects": false,
5
5
  "description": "React components for translations",
6
6
  "main": "./dist/index.cjs",
@@ -63,19 +63,17 @@
63
63
  },
64
64
  "dependencies": {
65
65
  "@babel/runtime": "^7.20.13",
66
- "@lingui/core": "4.4.1",
67
- "use-sync-external-store": "^1.2.0"
66
+ "@lingui/core": "4.4.2"
68
67
  },
69
68
  "devDependencies": {
70
69
  "@lingui/jest-mocks": "*",
71
70
  "@testing-library/react": "^14.0.0",
72
71
  "@types/react": "^18.2.13",
73
- "@types/use-sync-external-store": "^0.0.3",
74
72
  "eslint-plugin-react": "^7.32.2",
75
73
  "eslint-plugin-react-hooks": "^4.6.0",
76
74
  "react": "^18.2.0",
77
75
  "react-dom": "^18.2.0",
78
76
  "unbuild": "^1.1.2"
79
77
  },
80
- "gitHead": "e7103c9f06a493e5871086e121f037309b0b742c"
78
+ "gitHead": "316a004ec82721fcceb8f3c4a5aeb4a48d367927"
81
79
  }