如何在 react-native 中获取单独的数组文件名"info.js"的值



我是反应原生的新手。我想从数组中获取值。我定义了包含来自"info.js"的数据的javascripts对象。我想使用卡从组件名称"showInfo.js"中.js信息中获取值。下面是我附加的代码。

"信息.js">

export const STUDENTS= [
   {
      id: 0,
      name: 'Tayyab Arshad',
      image: 'images/tayyab.png',
      College: 'Ncba&e',
      Degree: 'BsCS',
      description: 'I am doing Bs Computer Science'
    },
    {
      id: 1,
      name: 'Abdul Wahab',
      image: 'images/abdul.png',
      College: 'Ncba&e',
      Degree: 'BsCS',
      description: 'I am doing Bs Computer Science'
    }

"显示信息.js">

import React, { Component } from 'react';
import { View } from 'react-native';
import { View, FlatList } from 'react-native';
import { Card, ListItem } from 'react-native-elements';
import { STUDENTS} from '../shared/info';
class Info extends Component {
    constructor(props) {
        super(props);
        this.state = {
            info: STUDENTS
        };
    }
    static navigationOptions = {
        title: 'Info'
    };
    render() {
        const renderInfoItem = ({item}) => {
            return (
                    <ListItem
                        title={item.name}
                        subtitle={item.description}
                        hideChevron={true}
                        leftAvatar={{ source: require('./images/tayyab.png')}}
                    />            
            );
        }
        return (
              <FlatList 
                data={this.state.info}
                renderItem={renderInfoItem }
              />
        );
    }
}

export default Info

我想从反应本机组件"showInfo.js"中的array(info.js(获取信息。 但我的实际代码不起作用。

您是否尝试在渲染方法中console.log(this.state.info),我认为您有数据,但它没有在FlatListrenderItem中列为错误

只需尝试此操作以确保它

render() {
    console.log("data :",this.state.info);
    ...
 }

你必须导入你想要渲染的元素(FlatListListItem(:

import { View, FlatList } from 'react-native';
import { Card, ListItem } from 'react-native-elements';

最新更新