Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trans component modifies passed tOptions and overrides "context" property from the object #1745

Closed
vasiliy-l opened this issue Apr 15, 2024 · 1 comment

Comments

@vasiliy-l
Copy link

馃悰 Bug Report

When rendering <Trans> component with tOptions property, the component modifies the given object by updating context value to the property passed to the component itself.

tOptions.context = context;

This behaviour is unexpected and wrong as, firstly, we modify passed tOptions inside the component, and secondly, we override the context value by undefined value (when context property is not specified for the <Trans>)

To Reproduce

https://codesandbox.io/p/sandbox/react18next-trans-toptions-override-issue-4f2kyg

const { t } = useTranslation();
const tOptions = useMemo(() => ({ context: "bar" }), []);

...

<Trans t={t} i18nKey="foo-key" tOptions={tOptions} />
// Rendering this component modifies the `tOptions` object by setting tOptions.context = undefined

...

<Trans t={t} i18nKey="foo-key" context="baz" tOptions={tOptions} />
// Rendering this component modifies the `tOptions` object by setting tOptions.context = "baz"

Expected behavior

The <Trans> component should not modify the given tOptions or mess with the context values.

This may be fixed in TransWithoutContext component, where combined options object is defined:

const combinedTOpts = {
...tOptions,
count,
...values,
defaultValue,
ns: namespaces,
};

const combinedTOpts = {
    ...tOptions,
    context: context || tOptions.context, // Add `context` from the props or fallback to the value from `tOptions`
    count,
    ...values,
    defaultValue,
    ns: namespaces,
  };

Your Environment

  • i18next version: 23.11.2
  • react-i18next version: 14.1.0
@adrai
Copy link
Member

adrai commented Apr 23, 2024

Should be addressed with v14.1.1

@adrai adrai closed this as completed Apr 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants