You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 18:21:00