无法访问JSON数据并仅打印Begin和End React Native

时间:2018-10-03 07:53:03

标签: json react-native react-native-android

我正在尝试访问JSON数据中的Begin和End,但是不能。它只是访问并打印JSON数据。

如何在单独的FlatList中打印开始和结束?

这是我的代码:

manage = date =>  {
 this.setState({status: true})
 this.setState({date:date})
const office_id=this.state.office_id;
const duration=this.state.duration;
  const  url='http://MyIP/api/timeApi';
  axios.post(url,{office_id,date,duration})
  .then(resp => this.setState({
      testArray : JSON.stringify(resp.data),
  }))
  .catch(e)
  {
      alert(e);
  }}

testFunction = () => {
    var x = this.state.testArray;
    return(

        <Text>{x}</Text>
    )

}

<DatePicker
                    date={this.state.date}
                    onDateChange={this.manage.bind(this) } />
  

作为回报

  { 
        this.state.status ?                 <View>

<FlatList 
  data={this.state.testArray}
     renderItem={this.testFunction}


/>
        </View> : null
    }

and here is my result

1 个答案:

答案 0 :(得分:0)

您需要类似的内容,请尝试根据您的代码进行更改

import React, { Component } from 'react';
import {Text, View, FlatList ,Button} from 'react-native';

export default class Test extends Component {
  constructor() {
    super();
    this.state = {
      data: ['one', 'two', 'three', 'four', 'five'],
    }
  }

  _keyExtractor = (item, index) => index.toString();

  _renderItem = ({ item }) => (
    <View>
      <Text>{item}</Text>
    </View>
  );

  manage = () => {
    var x = this.firstAndLast(this.state.data);
    this.setState({
      data : x
    })
  }

   firstAndLast(myArray) {

    var firstItem = myArray[0];
    var lastItem = myArray[myArray.length-1];
    var newArray = [];

    newArray.push(firstItem);
    newArray.push(lastItem);
    
    return newArray;
    }

  render() {
    return (
      <View>
        <FlatList
          data={this.state.data}
          extraData={this.state}
          keyExtractor={this._keyExtractor}
          renderItem={this._renderItem}
        />
        <Button title="click" onPress={() => this.manage()} />
      </View>

    );
  }
}