Cart. Please revi" /> Cart. Please revi" /> Cart. Please revi"/>

Unable to render link using react-intl

42 Views Asked by At
const msgid =
  'Not all item(s) could be added to your <a title="Cart" href="http://localhost:4502/homepage.html" target="_blank">Cart</a>. Please review error(s) below. ';
const msg =
  'Not all item(s) could be added to your <a title="Cart" href="http://localhost:4502/homepage.html" target="_blank">Cart</a>. Please review error(s) below. ';

<FormattedMessage id={msgid} defaultMessage={msgid}>
  {data => <span dangerouslySetInnerHTML={{ __html: data }} ></span>}
</FormattedMessage>

Version: "react-intl": "^5.20.13",

Only text is displayed without the link. Why?

1

There are 1 best solutions below

4
Taiwei Tuan On

The id supposed to be a string of a key instead of a variable. Therefore, to fix your issue, you need to simply update your code with the following

<FormattedMessage id={'msgid'} defaultMessage={msg}>
  {(data) => <span dangerouslySetInnerHTML={{ __html: data }}></span>}
</FormattedMessage>

See your code in action here: https://codesandbox.io/s/sharp-frost-76fsxl?file=/src/App.tsx

Read more about FormattedMessage component: https://formatjs.io/docs/react-intl/components/#formattedmessage