关于reactjs:如何将CSV读取到React Native State Array中?

How to read CSV into React Native State Array?

我有一个问题:如何将CSV中的数据加载到React中的数组中-特别是在this.state中作为对象。

让我们从这个开始,这里的状态只是硬编码的值。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
export default class App extends Component<{}> {
  constructor(props) {
    super(props);
    this.state = {
      cards: [
        {itemname: 'Item 1',
          restoname: 'Resto 1',
          url: 'https://res.cloudinary.com/grubhub/image/upload/v1515191165/bkf4niv9okipbc8y6o8h.jpg',
          description: 'Desc 1'},
        {itemname: 'Item 2',
          restoname: 'Resto 2',
          url: 'https://res.cloudinary.com/grubhub/image/upload/v1514060352/twhriy3hvbzayktrjp91.jpg',
          description: 'Desc 2'}
      ]
    };
  }

我是否可以通过这种方式从数据中读取数据而不是从CSV中读取数据?

enter

1
2
3
4
5
6
7
8
9
10
11
import csv from 'csvtojson';
const csvFilePath='<path to csv file>';
csv()
.fromFile(csvFilePath)
.on('json',(jsonObj)=>{
    // combine csv header row and csv line to a json object
    // jsonObj.a ==> 1 or 4
})
.on('done',(error)=>{
    console.log('end')
})


我认为您不能在React Native环境上使用csvtojson
它可以在节点上运行,但不能在Expo上运行,我认为对于RN来说是相同的。

如果您只需要本地数据,则将CSV转换为JSON并导入。无需额外的库。
我认为这是最简单的。

https://stackoverflow.com/a/59746430/1593212