javascript - 将 JSON 文件从 LocalStorage 解析为 react-native

标签 javascript json reactjs react-native local-storage

我一直在尝试从我的本地 JSON 文件中提取数据,但没有成功。我几乎尝试了所有方法,但没有用。我是否遗漏了什么或只是没有做对?如果有人能解决我的问题,我将非常高兴。通常在错误管理器上,会显示错误: [ts] JSX 表达式必须有一个父元素 [2657] ,但没有显示问题出在哪一行。

HistoryScreen.js

const Page = ({
      fromLeft,
      type,
      parallaxOn,
      flipSide,
      nextType,
      openDrawer,
    }) => (

      <View style={styles.page}>
        <RectButton style={styles.rectButton} onPress={openDrawer}>
            <View style={{flex: 1, flexDirection: 'row', justifyContent: 'space-between'}}>
                <FlatList 
                  data={this.state.dataSource}
                  renderItem = { ( {item}) => {
                return( 
                  <View style={{width: 60}}>
                    <Avatar

                      style={{flex: 0}}
                      size='large'
                      rounded
                      activeOpacity={0.7}
                      source={require('../components/avatars/club1.png')} />
                </View>
                <View>
                  <View style={{flex: 1, flexDirection: 'row', justifyContent: 'space-between'}}>
                      <View style={styles.rectButtonText}><Text>{item.name}</Text></View>
                      <View style={styles.rechtButtonTextNoteNote}><Text note >{item.date}</Text></View>
                  </View>
                  <Text note style={styles.rechtButtonTextNote}>{item.info}</Text>
                </View>
                    )
        }}
        keyExtractor = {(item, index) => index.toString()}
        />
        </View>
        </RectButton>

HistoryScreen.js

  export default class History extends Component {
  constructor(props){
    super(props);

    this.state = {
      isLoading: true, // check if json data (online) is fetching
      dataSource: [], // store an object of json data
    };
  }

  componentDidMount(){
      // set state value
      this.setState({
        isLoading: false , // already loading
        dataSoure : data.info
      });
  }

这是我的 JSON 文件 info.json

{
"info-club" : [
    {"name" : "Club-Defense Veterans" , "info" : "Pistolet , Carabine..." , "date" : "Fri May 04 2012 14:42:07 GMT-0070 (PDT)"},
    {"name" : "Club-Reflex Shooting" , "info" :  "Pistolet , Carabine...", "date" : "Fri May 04 2012 14:42:07 GMT-0070 (PDT)"},
    {"name" : "Club-Super Shooting" , "info" : "Pistolet , Carabine...", "date" : "Fri May 04 2012 14:42:07 GMT-0070 (PDT)"}
  ]
}

最佳答案

您好,FlatList 组件的 data 属性只接受值的数组。但是您正在 info.json 文件中生成一个对象。你可以尝试这样的事情。如果您知道要呈现的 json 文件的属性名称就足够了。

我创建了一个 minimal Expo example您可以在哪里看到它的工作原理。

import * as React from 'react';
import { 
  View, 
  StyleSheet, 
  TextInput,
  Dimensions,
  FlatList
} from 'react-native';

export default class App extends React.Component {

  constructor(props){
    super(props);
    this.state={
      data: require('./info.json') // load the file
    }
  }

  render() {
      const { data } = this.state   // state in a local const
      return (
          <View style={styles.container}>   
          <FlatList 
             data={data['info-club']}    // info-club prop of the json object
             keyExtractor={(item, index) => index.toString()}
             renderItem={ ({item, index}) => {

               // you custom logic here

               return (
                  <View>   { /*   I think you are missing that View Tag */}
                     <View style={{width: 60}}>
                         <Avatar
                           style={{flex: 0}}
                           size='large'
                           rounded
                           activeOpacity={0.7}
                           source={require('../components/avatars/club1.png')}
                         />
                     </View>
                     <View>
                        <View style={{flex: 1, flexDirection: 'row', justifyContent: 'space-between'}}>
                            <View style={styles.rectButtonText}><Text>{item.name}</Text></View>
                            <View style={styles.rechtButtonTextNoteNote}><Text note >{item.date}</Text></View>
                         </View>
                         <Text note style={styles.rechtButtonTextNote}>{item.info}</Text>
                       </View>
                  </View>
                )
             }}
          >  
         </FlatList>         
          </View>
      );
    }
}

关于javascript - 将 JSON 文件从 LocalStorage 解析为 react-native,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53746886/

相关文章:

javascript - 动态更新 Google Geochart 的颜色

javascript - 控制台不打印键值

mysql - phpMyAdmin 内部服务器错误 500 - 无效的 JSON

javascript - 使用来自外部 API 的传单解析 GeoJSON

javascript - 如何使用具有嵌套属性的对象传播?

javascript - 当Ajax从服务器获取数据时,字体端还是后端应该创建html输出

javascript - 如何将 52 个随机生成的数字打印到控制台?

javascript - Mongoose 从 html datepicker 中查找 2 个日期之间的结果

javascript - 如何检查 reactjs 中单个复选框的状态( Material 表)

javascript - 如何在 React 16.6.3 中调试 Lazy、Suspense 回退