1

I'm willing to parse the above JSON response to a FlatList, but I can't figure what I'm missing. Since it does not have a key and value pair, I'm not sure how to render it.

{"list":["a","b","c","d"]}

My code is...

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

export default class Home extends React.PureComponent {
  constructor(props) {
    super(props);
    this.state = { dataSource: [] };
  }

  async componentDidMount() {
    const response = await fetch('http://.../');
    const responseJson = await response.json();
    this.setState({ dataSource: responseJson.list });
  }

  render() {
    return (
      <View style={{ flex: 1, paddingTop: 20 }}>
        <FlatList
          data={this.state.dataSource}
          renderItem={({ item, index }) => <Text>{item[index]}</Text>}
          keyExtractor={(item, index) => index}
        />
      </View>
    );
  }
}

3
  • I can see a typo here this.setState({ dataSource: responseJson.lista }); you don't have any property named lista in your json Commented Feb 22, 2019 at 2:11
  • Sure! I corrected, but still cant parse. Commented Feb 22, 2019 at 2:13
  • Also You don't need to index here renderItem={({ item, index }) => <Text>{item[index]}</Text>} facebook.github.io/react-native/docs/flatlist Commented Feb 22, 2019 at 2:18

2 Answers 2

2

Your problem is because you're doing the below

renderItem={({ item, index }) => <Text>{item[index]]}</Text>}

item is referring to a,b,c,d but you're doing a[index],b[index] which clearly wrong

Solutions:

<FlatList
...
  renderItem={({ item }) => <Text>{item}</Text>}
...
/>

You don't need index for your renderItem because the item is already a,b,c,d respectively

Sign up to request clarification or add additional context in comments.

5 Comments

I'll try it so!
@issac I also think this is your problem. In addition, you could read this article to examine some examples medium.com/react-native-development/…
@vuluu: Sorry I don't follow you
Thank you for the help!
@ArthurÁvila: Do mark it as answer if it helps to fix your problem as a reference for future reader stumbled on same issue. Glad to help thanks
0

You dont need item index, try the below code and let me know.

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

export default class Home extends React.PureComponent {
  constructor(props) {
    super(props);
    this.state = { dataSource: [] };
  }

  async componentDidMount() {
    const response = await fetch('http://.../');
    const responseJson = await response.json();
    this.setState({ dataSource: responseJson.list });
  }

  render() {
    return (
      <View style={{ flex: 1, paddingTop: 20 }}>
        <FlatList
          data={this.state.dataSource}
          renderItem={({item}) => <Text>{item}</Text>}
          keyExtractor={this.state.dataSource.indexOf(item)}
        />
      </View>
    );
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.