【问题标题】:Fetch data json in react-native在 react-native 中获取数据 json
【发布时间】:2020-04-21 02:16:17
【问题描述】:

我想问 在我使用 Flatlist 显示数据之前,我能够使用 Fetch 方法显示 Api 数据并且它可以工作。然后我使用时间轴自定义显示,当我将数据渲染到视图中时,为什么它没有出现?

我的代码

 constructor(){
      super()
      this.state = {
          data: [],
          isLoading : true
      }
  }
    fetchData = async() =>{
        const { params } = this.props.navigation.state;
        const response =  await fetch('https://example.com/get_by?id_order='+ params.id);
        const json = await response.json(); // products have array data
        this.setState({data: json.data}); // filled data with dynamic array
    };
    componentDidMount(){
        this.fetchData();
  };

  renderDetail() {
    let title = <Text style={[styles.title]}>{this.state.item.awb}</Text>
    var desc = null
    if(this.state.description)
      desc = (
        <View style={styles.descriptionContainer}>   
           <Text style={[styles.textDescription]}>{this.state.description}</Text>
        </View>
      )

    return (
      <View style={{flex:1}}>
        {title}
        {desc}
      </View>
    )
  }

  renderDetail() {
    let title = <Text style={[styles.title]}>{this.state.item.awb}</Text>
    var desc = null
    if(this.state.status)
      desc = (
        <View style={styles.list}>   
           <Text style={[styles.list]}>{this.state.status}</Text>
        </View>
      )

    return (
      <View style={{flex:1}}>
        {title}
        {desc}
      </View>
    )
  }

    render(){
        const { params } = this.props.navigation.state;
        const {navigate} = this.props.navigation;
        const {timeline} = this.state;

       return(
        <View style={styles.container}>
        <Text style={styles.pageName}>{params.id}</Text>
         <Timeline renderDetail={this.renderDetail}

            circleSize={20}
            separator={true}
            circleColor='blue'
            lineColor='gray'
            timeStyle={styles.time}
            descriptionStyle={styles.description}
            style={styles.list}
            data={this.state.data}
          />
       </View>
       );
   }

我的代码有问题吗? 我试过这样,还是不行

【问题讨论】:

  • 让我们看看您是如何导入时间轴的
  • 从 'react-native-timeline-flatlist' 导入时间线;
  • 正如您在Documentation 中看到的那样,renderItem 不是有效的道具
  • 先生能举个例子吗?谢谢

标签: json reactjs react-native


【解决方案1】:

您可以简单地将data={this.state.data} 添加到您的Timeline 组件中。排序如下:

<Timeline data={this.state.data}  ...otherprops />

更新:

const mappedData = this.state.data.map((item => {
 return {
   time:item.date,
   title:item.awb,
   description:item.status
 }
}))

<Timeline data={mappedData}  ...other props />

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-15
  • 2021-10-19
相关资源
最近更新 更多