是否可以创建通用的React列表组件?

时间:2019-04-29 17:11:32

标签: reactjs

我才刚开始使用React和ES6,我想稍微干燥一下我的应用程序。我想要做的是创建一个组件,该组件接受一个集合以及该集合中各项的属性,然后将其转换为列表。例如,如果我传入一组作者并指定姓氏,它将创建一个作者的姓氏列表。使用相同的组件,我想在其他地方使用它,并传递一组歌曲,然后按标题列出它们。

这是我到目前为止所拥有的:

ItemList

import PropTypes from 'prop-types'
import React from 'react'

import Item from './Item'

export default class ItemList extends React.Component {
  constructor(props){
    super(props)
  }

  render() {
    let items

    if(this.props.items.length === 0){
      items = <div>Nothing Found</div>
    } else {
      items = this.props.items.map(item => {
        return(
          <Item item={item} displayAttribute={this.props.displayAttribute}
        )
      });
    }

    return (
      <div className="item-index">
        <div className="list-group">{items}</div>
      </div>
    );
  }
}

项目:

import PropTypes from 'prop-types'
import React from 'react'

export default class Item extends React.Component {
  constructor(props){
    super(props)
  }

  render() {
    return (
      <div className="list-group-item" data-index={this.props.item.id}>
        <div className="item-attribute">
          *This is where I want to print the item's display attribute*
        </div>
      </div>
    );
  }
}

在应用程序的其他地方,我希望能够调用以下内容:

<ItemList items={this.state.authors} displayAttribute="last_name" />

<ItemList items={this.state.songs} displayAttribute="title" />

,组件将使用指定的属性创建一个列表

1 个答案:

答案 0 :(得分:2)

如果我对您的理解正确,那么应该这样做(在您的商品列表中):

insertquery="""insert /*+ append_values parallel(XYZ,8) */ into XYZ(A,B,C,D,E,F,G,H, I,J,K,L,M,N,O,P,Q,R,S)             VALUES('abc',:B,'N',:D,:E,:F,:G,:H,:I,:J,:K,:L,:M,:N,:O,'MIG','MIG',{current_time},{current_time})""".format(current_time=current_time)