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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trans component should NOT modify the input props value. #1733

Open
Jayatubi opened this issue Mar 8, 2024 · 1 comment
Open

Trans component should NOT modify the input props value. #1733

Jayatubi opened this issue Mar 8, 2024 · 1 comment

Comments

@Jayatubi
Copy link

Jayatubi commented Mar 8, 2024

I've got an issue around this line in TransWithoutContext.js:

components[c] = createElement(Componentized);

  if (components) {
    Object.keys(components).forEach((c) => {
      const comp = components[c];
      if (
        typeof comp.type === 'function' ||
        !comp.props ||
        !comp.props.children ||
        (translation.indexOf(`${c}/>`) < 0 && translation.indexOf(`${c} />`) < 0)
      )
        return;

      // eslint-disable-next-line react/no-unstable-nested-components, no-inner-declarations
      function Componentized() {
        // <>{comp}</>
        return createElement(Fragment, null, comp);
      }
      // <Componentized />
      components[c] = createElement(Componentized);
      // ^ `components` may be frozen and then crash happens
    });
  }

Looks like Trans was going to modify the input prop components inplace. However, this may lead a crash while the components is readonly, aka frozen.

I encounter this issue by accidentally storing a React component with Trans by using useImmer hook. The useImmer hook will freeze all the states in production environment by default.

For exmaple:

const MyComponent = () => {
    // The components props will be frozen in production environment
    const [Inner, setInner] = useImmer(<>
        <Trans components={components}></Trans>
    </>)

    return <Inner/>;
}

So instead of using forEach to convert the input components may be it would be better to use map to create a new variable for later use.

@Jayatubi Jayatubi changed the title Trans component should NOT to modify the input props value. Trans component should NOT modify the input props value. Mar 8, 2024
@adrai
Copy link
Member

adrai commented Mar 8, 2024

Feel free to provide a PR to address this.

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