【问题标题】:How to sync when render a component?渲染组件时如何同步?
【发布时间】:2020-09-21 12:28:12
【问题描述】:

当我尝试捕获新数据时遇到问题,我更改了内容中的某些内容并呈现它。

import React, { Component } from 'react'
import { withNamespaces } from "react-i18next";

class Untitled extends Component {
  constructor(props) {
    this.columns = [...content]
  }
  render() {
    const columns = this.columns.map((col) => {
      return {
        ...col,
        onCell: (record) => ({
          record,
          dataIndex: col.dataIndex,
          title: col.title,
        }),
      };
    });
    return (
      <div>
        {columns}
      </div>
    )
  }
}

export default Untitled

在这里,当我更改 [...content] 中的某些值时,我希望“const 列”将更新这些新值并将其呈现在 return() 中。我确信当 [...content] 更新一个新值时,render() 中有更新。但是“const 列”仍然保留旧值。

P/s:我不能像 this.state { columns: [] } 这样声明 this.columns,因为我不想更改旧开发的代码。正是我想在切换语言时在 this.columns 中获取新值 this.props.t("TEST")。

非常感谢。

【问题讨论】:

    标签: reactjs i18next


    【解决方案1】:

    您可以使用this.forceUpdate() 手动触发重新渲染 .你可以在这里得到更多的解释https://reactjs.org/docs/react-component.html#forceupdate

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-27
      • 1970-01-01
      • 2013-01-20
      • 2019-07-21
      • 2022-07-27
      • 2019-10-31
      • 1970-01-01
      相关资源
      最近更新 更多