【问题标题】:<FormattedMessage /> return string without any tags<FormattedMessage /> 返回不带任何标签的字符串
【发布时间】:2018-10-15 01:43:06
【问题描述】:

请耐心等待,我不知道如何解释这个问题。

我正在使用 Bootstrap 表来显示我的数据,我在表中有一个 searchFilters、排序和更多功能。

当我开始实施国际化时,我首先在文本组件中直接格式化,当我开始获得太多列和表时,我将列名移动到需要时读取的外部文件中。正如你在 headerFormatter 中看到的那样,我还需要添加动态翻译。

问题是react-intl 库在渲染中添加了&lt;span&gt; 标记,这会导致其他&lt;span&gt; 标记无法正常工作。

const headerFormatter = (col, colIndex) => {
    return ( 
        <FormattedMessage id={col.text} key={colIndex} />
    );
}
//Column Example
{dataField: 'agentNo',text: 'AgentNo', headerFormatter: headerFormatter, filter: textFilter({defaultValue:''}), sort: true},

有没有办法让&lt;FormattedMessage /&gt; 在这种情况下只返回一个string?

额外信息

格式化:

<span>Date</span>

非格式化:

AgentNo
<span class="react-bootstrap-table-sort-order dropup"><span class="caret"></span></span>

这个&lt;span&gt; 被&lt;FormattedMessage /&gt; 的&lt;span&gt; 压垮了

【问题讨论】:

  • 你正在返回一个组件 &lt; FormattedMessage &gt; 为什么你想要一个字符串?你会用字符串做什么?我不确定我是否理解您的问题
  • 整个东西都在一个无状态元素中,我想要一个字符串,因为它会覆盖列标题中的其他跨度功能
  • 该列位于export const entityListColumns = { columns : [下
  • 我在问题中添加了更多细节

标签: javascript reactjs internationalization bootstrap-4 react-intl


【解决方案1】:

我们决定重组并使用基于上下文的函数formatMessage

【讨论】:

    【解决方案2】:

    您可以像这样将您的类添加到 FormattedMessage 组件中:

    const headerFormatter = (col, colIndex) => {
        return ( 
            <FormattedMessage className="react-bootstrap-table-sort-order dropup" id={col.text} key={colIndex} />
        );
    }

    【讨论】:

    • class 是 React 中的保留关键字。
    • 您可以写className 进行造型
    • MayK 这也行不通,因为css class 是由bootstrap 动态生成的,并且当排序状态更改为按升序或降序排序时会发生变化。它将从react-bootstrap-table-sort-order dropup 变为react-bootstrap-table-sort-order
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-04
    • 2011-10-04
    相关资源
    最近更新 更多