React中如何在模板字符串里添加HTML标签?
Hey there! I get what you're aiming for—you want the "Warning" text rendered in bold, while the rest of your message stays in normal formatting, all using your t() translation function.
The problem with your current code is that you're trying to concatenate a JSX element (<strong>...</strong>) into a template string. In React, JSX elements are objects, so when you do that, you'll end up seeing [object Object] instead of the bold warning text.
Let's fix this properly with React's native JSX syntax:
Option 1: Directly combine elements in a JSX fragment
This is the most concise approach:
const helperMessage = ( <> <strong>{t('Warning')}</strong> {t('phrases:updating your name can impact your production environment.')} </> );
Option 2: Keep the warning as a reusable variable
If you want to reuse the bold warning elsewhere, define it separately and then include it in your JSX:
const warning = <strong>{t('Warning')}</strong>; const helperMessage = ( <> {warning} {t('phrases:updating your name can impact your production environment.')} </> );
Both approaches work because we're using a JSX fragment (<>...</>) to group the bold element and the translated text. This tells React to render them as a single cohesive block, preserving the bold formatting for "Warning" while keeping the rest of the text normal. Your t() translation function will operate exactly as intended here, since we're passing the translation keys directly into the JSX context where they belong.
内容的提问来源于stack exchange,提问作者Name

