react-i18next icon indicating copy to clipboard operation
react-i18next copied to clipboard

Overriding React component props

Open Jastrzebowski opened this issue 1 year ago • 4 comments

Documentation issue

I have an issue with the Trans Component Overriding React component props behaviour. I checked the Trans tests, and there is no test for Overwriting props; once I add one, it's failing. Is it a feature removed from the code but left in the documentation?

For

/**
 * With messages:
 * myKey: 'This is a <CustomLink href="https://example.com/en-EN">link to example.com</CustomLink>.'
 **/  

<Trans
  i18nKey="myKey"
  components={{ 
    CustomLink: <a href="value-to-be-overridden"/> 
    }}
/>

Existing behaviour

This is a <a href="value-to-be-overridden">link to example.com</a>.

Expected behaviour

This is a <a href="https://example.com/en-EN">link to example.com</a>.

Motivation

I want to fix the flows in the documentation and, at best, bring back the props substitution. The use case with links seems valuable, especially when using Routing. However, I can see additional ones, such as using icons as a component.

Jastrzebowski avatar Sep 16 '24 22:09 Jastrzebowski

Here is the test: https://github.com/i18next/react-i18next/blob/badba254ccdd16145f5b0628babaad1efffc8448/test/trans.render.spec.jsx#L106 with this key: https://github.com/i18next/react-i18next/blob/master/test/i18n.js#L44

adrai avatar Sep 17 '24 05:09 adrai

Thank you, @adrai! The test is different than the examples in the documentation with custom components. The corresponding one I was looking at yesterday is: https://github.com/i18next/react-i18next/blob/badba254ccdd16145f5b0628babaad1efffc8448/test/trans.render.spec.jsx#L131

Would that mean the costume tags were never meant to have overwriteable props?

Jastrzebowski avatar Sep 17 '24 08:09 Jastrzebowski

Feel free to provide a PR for that.

adrai avatar Sep 17 '24 08:09 adrai

https://github.com/i18next/react-i18next/blob/badba254ccdd16145f5b0628babaad1efffc8448/test/trans.render.spec.jsx#L91 if the code has no prop it should pick from translation string...

honestly...idk...but setting props from translations is another pandora's box

If I remember right sole reason to introduce this was to allow for some keepBasicHtmlTags that contained links...but not to allow full props replacement (therefore also component props if existing win over translations)

jamuhl avatar Sep 17 '24 08:09 jamuhl

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Apr 26 '25 02:04 stale[bot]

I ran into this issue too today. The documentation is wrong, and it seems that any props defined in the components take precedence over values parsed from the translation string.

<Trans
  i18nKey="myKey"
  components={{ 
    CustomLink: <a /> 
    }}
/>

Seems to provide the expected result (the href from the translation string will be used)

sam-shift72 avatar Jun 30 '25 01:06 sam-shift72