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

React中如何让Tooltip内的对象值每行显示一个

Solution for Line Breaks in React-Tooltip with Multiple Errors

Since you've already enabled the html prop on ReactTooltip, we can leverage that to render proper line breaks between your error messages. Here's a straightforward fix:

Step 1: Format Error Messages with HTML Line Breaks

Instead of passing the raw array from Object.values(item.error), we'll join each error with an HTML <br> tag. The html prop on your ReactTooltip will parse this as actual line breaks instead of plain text.

Update your tooltip call code like this:

{ columnTitles.map((item, index) => (
  <th key={index} className="regular center" style={{ height: 80 }}>
    { item.error && (
      <Tooltip 
        text={
          Object.values(item.error).length 
            ? Object.values(item.error).join('<br>') 
            : 'Error no especificado'
        } 
        type="warning" 
        element={ <span className="color-dark-grey">&#x26A0;</span> } 
      /> 
    ) }
    <span className="inline-block truncate h6 align-middle"> {item.title} </span>
  </th>
)) }

Step 2: Confirm Tooltip Component Setup

Your existing Tooltip component is already correctly configured with the html prop, which is critical for making this work. No changes needed here, but just to validate:

class Tooltip extends PureComponent {
  render() {
    const { text, element, type, place } = this.props;
    return (
      <div data-tip={text} className="m0 p0">
        {element}
        <ReactTooltip type={type} place={place} effect="solid" className="tooltip-top" html />
      </div>
    );
  }
}

Alternative: Use a Bulleted List for Better Readability

If you want more structured error display, you can format the errors into an unordered list instead:

text={
  Object.values(item.error).length 
    ? `<ul>${Object.values(item.error).map(err => `<li>${err}</li>`).join('')}</ul>` 
    : 'Error no especificado'
}

This will render each error as a separate list item, which can be easier to scan for longer sets of errors.

内容的提问来源于stack exchange,提问作者Lizz Parody

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 06:18:30