Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将数据追加到react中的状态数组中_Javascript_Reactjs_React Native - Fatal编程技术网

Javascript 将数据追加到react中的状态数组中

Javascript 将数据追加到react中的状态数组中,javascript,reactjs,react-native,Javascript,Reactjs,React Native,我试图将data.rate附加到this.state.test中,在迭代该值之后,问题是数据没有显示在测试中 导出默认类LiveApiData扩展组件{ 构造函数(){ 超级(); this.state={nda:[],test:[]}; } componentDidMount(){ 提取(“”) .then((response)=>response.json()) .then((data)=>this.setState({nda:data})); } render(){ 设x=this.sta

我试图将data.rate附加到this.state.test中,在迭代该值之后,问题是数据没有显示在测试中

导出默认类LiveApiData扩展组件{
构造函数(){
超级();
this.state={nda:[],test:[]};
}
componentDidMount(){
提取(“”)
.then((response)=>response.json())
.then((data)=>this.setState({nda:data}));
}
render(){
设x=this.state.ndata.map((data)=>this.setState({test:data.rate}));
返回(
{x}
);
}
}
console.log之后的api示例

0:{timestamp:"2018-04-14T00:00:00Z",rate:"7999.09816231845214650049179249741253"}
►1:{timestamp:"2018-04-15T00:00:00Z",rate:"8357.6586830697213301743207363910892"}
►2:{timestamp:"2018-04-16T00:00:00Z",rate:"8046.07792934854261706414967686716349"}

如果您的测试只是状态的派生值,那么您不需要为它指定状态

export default class LiveApiData extends Component {
  constructor() {
    super();
    this.state = { ndata: [] };
  }

  componentDidMount() {
    fetch('')
      .then((response) => response.json())
      .then((data) => this.setState({ ndata: data }));
  }

  render() {
    let x = this.state.ndata.map((data) => { test: data.rate });
    return (
      <View>
        <Text> {x} </Text>
      </View>
    );
  }
}

假设在
中接收到
数据
,则
调用是一个对象数组,每个对象都包含速率

componentDidMount() {
    fetch('')
      .then((response) => response.json())
      .then((data) => {
              const ndata = data
              const test = data.map((element)=>element.rate)

              this.setState({ndata:ndata, test:test})
           });
}

那么,问题是什么problem@Irfanwani问题是this.state中的测试未显示任何内容。数据是否已成功保存到最初装载的ndata状态阵列?样本数据是对象响应的结构吗?@twominds是的。响应是以对象数组的形式出现的吗?
componentDidMount() {
    fetch('')
      .then((response) => response.json())
      .then((data) => {
              const ndata = data
              const test = data.map((element)=>element.rate)

              this.setState({ndata:ndata, test:test})
           });
}