javascript - react 自定义组件(数据表)

标签 javascript reactjs typescript asp.net-core react-redux

我是 React 的新手,我正在尝试构建带有参数的自定义组件。

我只是想知道这样做的确切方法是什么。

这是我当前的代码,我应该如何将那些 Columnsajaxdatasource 传递给 componenet。 还是我做错了?

import * as React from 'react';
interface Column {
    name: string,
    header: string,
    value: Function
}
export default class DataTable extends React.PureComponent<({
    dataSource: any[],
    ajax: string
    columns: Column[]
    onEdit: Function,
    onDelete: Function
})>{

    public state = {
        dataSource: [],
        ajax: undefined,
        columns: [],
        onEdit: undefined,
        onDelete: undefined
    }

    componentDidMount() {
        if (this.state.ajax != undefined) {
            fetch(this.state.ajax)
                .then(response => response.json())
                .then(data => this.setState({ dataSource: data }));
        }
    }

    render() {

        var template = (
            <table className="table">
                <thead className="thead-darked">
                    {
                        this.state.columns.map((x: Column) => {
                            <th scope="col">{x.header}</th>
                        })
                    }
                </thead>

            </table>)
        return template;
    }
} 

最佳答案

  1. 您不需要 ajaxcolumnsonEditonDelete 状态。只需使用 Prop 。
  2. this.props.dataSource初始化this.state.dataSource

以上两项更改将解决您的问题。此外,

  1. 如果 ajax 属性发生变化,您可能需要再次获取数据。您必须为该行为实现 componentDidUpdate 方法。但是,我建议制作一个函数组件并使用 useEffect Hook 。
  2. 如果 dataSource 属性发生变化,您的 dataSource 状态将不会更新。尽管您可以添加更多代码来避免错误,但它可能会导致错误行为。如果将“ajax”部分移到组件之外,只使用 dataSource prop,逻辑会更清晰,更不容易出错。

下面是更新后的代码。

interface Props {
    dataSource: any[],
    ajax: string,
    columns: Column[],
    onEdit: Function,
    onDelete: Function
}

export default class DataTable extends React.PureComponent<Props>{
  public state = {
    dataSource: this.props.dataSource,
  }

  componentDidMount() {
    if (this.props.ajax != undefined) {
      fetch(this.props.ajax)
        .then(response => response.json())
        .then(data => this.setState({ dataSource: data }));
    }
  }

  render() {
    const template = (
      <table className="table">
        <thead className="thead-darked">
          {
            this.props.columns.map((x: Column) => {
              <th scope="col">{x.header}</th>
            })
          }
        </thead>
        {/* render something with this.state.dataSource */} 
      </table>
    );
    return template;
  }
} 

关于javascript - react 自定义组件(数据表),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58891533/

相关文章:

reactjs - 如何在 Next.js 静态站点生成上获取 URL 查询字符串?

javascript - react map 函数: how to output content of all nested arrays at once?

javascript - React - 显示 firestore 时间戳

typescript - 如何扩展类型声明?

javascript - 如何在循环中使用正则表达式替换来更改文本

javascript - Backbone 路由器路由定义来处理所有这些情况

javascript - 未捕获的范围错误 : Maximum call stack size exceeded

javascript - 仅复制对象的一部分的优雅方式

Angular 8 : How to provide different service instances to multiple instances of the same component

node.js - 尝试在 Webstorm 中将 app.js 转换为 Typescript