简体   繁体   English

反应中的通用成分

[英]Generic Components in react

I want to create a generic component that renders a table. 我想创建一个呈现表的通用组件。 The issue is that each table shall have different information each time. 问题是每个表每次都应具有不同的信息。 For example I want to use the same component for rendering a table of users (with fields username and email) and a table of companies (with fields: companyName, country, numberofEmployees). 例如,我想使用相同的组件来呈现用户表(具有用户名和电子邮件字段)和公司表(具有公司名称,国家/地区,numberofEmployees)。 I have an idea on how to do this, but since I am a beginner in React I need some help. 我对如何执行此操作有一个想法,但是由于我是React的初学者,因此需要一些帮助。

While no one here is going to be able to walk you through building an entire Table component, I would suggest making a Table component and build it in a composable way, meaning what it looks like will be based off the props are passed in. There is usually a lot to consider and once you start building it and have code to show then we can be more useful to you here. 虽然这里没有人能够引导您完成整个Table组件的构建,但我建议您制作一个Table组件并以可组合的方式构建它,这意味着它看起来像是基于传入的道具。通常需要考虑很多事情,一旦开始构建它并有代码演示,我们在这里对您会更加有用。

There are also third party libraries that handle this for you if you are needing something quickly. 如果您需要快速解决的问题,也可以使用第三方库为您处理。 Otherwise, build it yourself and have fun while learning React. 否则,请自己构建它,并在学习React的同时获得乐趣。 http://griddlegriddle.github.io/Griddle/ Griddle is a library i have used before and it got the job done. http://griddlegriddle.github.io/Griddle/ Griddle是我以前使用过的一个库,它可以完成工作。

a simple example using react to create a table and a generic component that renders a table: 一个使用react创建表和呈现表的通用组件的简单示例:

a generic component that renders a table 呈现表格的通用组件

import React from 'react';

class App extends React.Component {
   constructor(props){
     super(props);
     this.state = {
       data: [
         {"id": 1, "name": "Fofu", "age": "21"},
         {"id": 2, "name": "Lulu", "age": "31"},
         {"id": 3, "name": "Juju", "age": "41"},
         {"id": 4, "name": "Tolo", "age": "51"},
         {"id": 5, "name": "kiki", "age": "61"},
         {"id": 6, "name": "Lili", "age": "71"},
         {"id": 7, "name": "Jiji", "age": "81"},
         {"id": 8, "name": "Titi", "age": "91"}
       ]
     }
   }

  render() {
     var myStyle = {
       fontSize: 16,
       color: '#AF0000'
     }A
      return (
         <div>
            <Header/>
            <table>
              <tbody>
                {this.state.data.map((person, i) => <TableRow key = {i} data = {person} />)}
              </tbody>  
            </table>  
        </div>
      );
   }
}

class Header extends React.Component {
  render() {
     var myStyle = {
       fontSize: 20,
       color: '#FF0000'
     }
     return (
        <div>
           <h1 style = {myStyle}> My New Table Example in React</h1>
        </div>
     );
  }
}

class TableRow extends React.Component {
  render() {
     var myStyle = {
       fontSize: 12,
       color: '#AF0000'
     }
     return (
        <tr>
            <th>Id: </th>
           <td style = {myStyle}>{this.props.data.id}</td>
           <th>Name: </th>
           <td style = {myStyle}>{this.props.data.name}</td>
           <th>Age: </th>
           <td style = {myStyle}>{this.props.data.age}</td>
        </tr>
     );
  }
}
export default App;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM