首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >数据内部的react-table呈现组件

数据内部的react-table呈现组件
EN

Stack Overflow用户
提问于 2019-09-11 04:01:05
回答 2查看 16.3K关注 0票数 10

我正在尝试使用react-table package (https://www.npmjs.com/package/react-table#example)在数据中添加一个组件。

使用自述文件包中的示例,我尝试使用一个奇特的组件将图像添加到单元格:使用**跳出代码...我也尝试过:

代码语言:javascript
复制
imageUrl:{<PlaceholderImage width={60} textColor="#fff" text="Image"/>}

示例:

代码语言:javascript
复制
import ReactTable from 'react-table'
import 'react-table/react-table.css'
**import { PlaceholderImage } from 'react-placeholder-image'**

render() {
  const data = [{
    name: 'Tanner Linsley',
    age: 26,
**imageUrl:<PlaceholderImage width={60} textColor="#fff" text="Image"/>,**
    friend: {
      name: 'Jason Maurer',
      age: 23,
    }
  },{
    ...
  }]

  const columns = [{
    Header: 'Name',
    accessor: 'name' // String-based value accessors!
  }, {
    Header: 'Age',
    accessor: 'age',
    Cell: props => <span className='number'>{props.value}</span> // Custom cell components!
  }, {
    Header: ' ',
    accessor: 'imageUrl', // String-based value accessors!
    maxWidth: 70,
    minWidth:70,
  },{
    id: 'friendName', // Required because our accessor is not a string
    Header: 'Friend Name',
    accessor: d => d.friend.name // Custom value accessors!
  }, {
    Header: props => <span>Friend Age</span>, // Custom header components!
    accessor: 'friend.age'
  }]

  return <ReactTable
    data={data}
    columns={columns}
  />
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-09-11 04:17:52

您正在将react组件传递给需要字符串的数据字段。尝试通过Cell props自定义您的手机:

代码语言:javascript
复制
const columns = [
  {
    Header: "Image",
    accessor: "imageUrl",
    maxWidth: 70,
    minWidth: 70,
    Cell: props => <PlaceholderImage width={60} textColor="#fff" text="Image" />
  }
];
票数 25
EN

Stack Overflow用户

发布于 2020-08-03 12:26:07

除了@Clarity的答案之外,还可以在Cell的属性级别访问单元格的值:

代码语言:javascript
复制
const columns = [
  {
    Header: "Image",
    accessor: "imageUrl",
    maxWidth: 70,
    minWidth: 70,
    Cell: ({ cell: { value } }) => (
      <img
        src={value}
        width={60}
      />
    )
  }
];
票数 14
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57877482

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档