简体   繁体   English

在响应本机中按时间限制迭代JSON数据

[英]Iterate JSON Data by some time limit in react native

Currently i have a <Text> values inside a <View> in react-native . 目前我在react-native<View>内部有一个<Text>值。 and i have to pass one JSON to iterate over that text field . 而且我必须传递一个JSON来遍历该文本字段。 so how i want them to be is , Assume the JSON contains 4 objects , So initially The text field will show the first object's value inside the <View> . 所以我希望它们是,假设JSON包含4个对象,因此最初,文本字段将在<View>显示第一个对象的值。 After 5 seconds instead of first object i have to show the Second object . 5秒后,我必须显示第二个对象,而不是第一个对象。 like wise it has to goes on until the last object come's . 就像明智的做法一样,它必须持续到最后一个对象到来为止。 For example , 例如 ,

  const mydata = [ { name:"Aaa" //object 1 }, { name:"Bbb" //object 2 }, { name:"Ccc" //object 3 }, { name:"Ddd" //object 5 }, ] //initial view <View> <Text>{object1.name} </Text> /// ---> Aaa </View> //After 5 seconds <View> <Text>{object2.name} </Text> /// ---> Bbb </View> //Like this it has to go on . 

and it need's to be change automatically for every 5 seconds .Here i attached my code , 并且需要每5秒钟自动更改一次。在此附上我的代码,

 <View> {allData.map((data,i) => ( <DataInDetail // this is the component i imported name={data.name} /> ))} </View> //DataInDetail component import React, { Component } from "react"; import { View, Text, Image, StyleSheet, Dimensions, Platform } from "react-native"; const win = Dimensions.get("window"); const width = win.width; export default class DataInDetail extends Component { render() { return ( <View> <Text>{this.props.name}</Text> </View> ); } } 

Now it is showing the last JSON only . 现在只显示最后一个JSON。 pls someone clarify me from this . 请有人从这澄清我。 Thanks in advance . 提前致谢 。

You can use setTimeout to achive this. 您可以使用setTimeout实现此目的。 Check TimerMixin to use this.setTimeout . 检查TimerMixin以使用this.setTimeout TimerMixin helps you with the state setting error after the components unmount. 卸载组件后,TimerMixin可帮助您解决状态设置错误。

Example

const data = [ { name:"Aaa" }, { name:"Bbb" }, { name:"Ccc" }, { name:"Ddd" } ];
export default class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      name: ''
    };
  }

  componentDidMount() {
    data.forEach((d, index) => {
      this.setTimeout(() => {
        this.setSTate({ name: d.name });
      }, (5000 * index))
    })
  }

  render() {
    return(
      <View>
        <DataInDetail name={this.state.name} />
      </View>
    )
  }
}

UPDATE (To make it loop indefinitely) You can turn the setTimeout loop into a function and can call it at the last index of the loop. UPDATE (使它无限期地循环)您可以将setTimeout循环转换为一个函数,并可以在循环的最后一个索引处调用它。 We increment index by one because we want change happen after 5 seconds after the initial start. 我们将index增加1,因为我们希望在初次启动后5秒后发生更改。

const data = [ { name:"Aaa" }, { name:"Bbb" }, { name:"Ccc" }, { name:"Ddd" } ];
export default class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      name: ''
    };
  }

  setLoop(isIntial) {
    data.forEach((d, index) => {
      setTimeout(() => {
        this.setSTate({ name: d.name }, () => {
          if (index == (data.length - 1)) this.setLoop.bind(this, false);
        });
      }, (isInitial === true ? (5000 * index) : (5000 * (index + 1) )));
    });
  }

  componentDidMount() {
    this.setLoop(true);
  }

  render() {
    return(
      <View>
        <DataInDetail name={this.state.name} />
      </View>
    )
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM