2

My Api returns me a response exactly like the const "devices" defined below. If i don't call the api, i am getting the listview properly populated with single row of data, but if i populate the datasource using Api response, i am getting empty 160 rows.

Whats wrong with my setState?

    const devices = [
        {
            "DeviceID": 4,
            "DeviceCode": "ODPRC-HB-4",
            "ContactName": "Sudhir",
            "GPS_Lat_Long": "15.396863, 74.012382",
            "DeviceName": "Sudhirs Device"
        }
    ];

    class Devices extends Component {

    constructor(props) {
        super(props);
        const ds = new ListView.DataSource({
            rowHasChanged: (r1, r2) => r1 !== r2,
        });
        this.state = {
            dataSource: ds.cloneWithRows(devices)
        }
    }
    componentDidMount() {
        this.getDeviceList();
    }

    getDeviceList() {
        let request = new Request('https://myapiWithQueryString.com');
        request.addHeaders({ 'Content-Type': 'application/json'});  
        RNETWORK.get(request, () => {
        }, (response) => {
            if (response.getError()) {
                alert(response.getError().message);
            } else {
                this.setState({
                    dataSource: this.state.dataSource.cloneWithRows(response.getBodyString())

                });
                alert(response.getBodyString());
            }
        });
    }

3
  • What is the exact value of response.getBodyString() ? An array or a string ? Commented Oct 20, 2017 at 15:08
  • exactly like this... [ { "DeviceID": 4, "DeviceCode": "ODPRC-HB-4", "ContactName": "Sudhir", "GPS_Lat_Long": "15.396863, 74.012382", "DeviceName": "Sudhirs Device" } ] Commented Oct 20, 2017 at 15:17
  • at the initial, there will be no data with devices, so how did u handle that? If you could share that will be very helpful Commented Jun 17, 2018 at 15:49

1 Answer 1

1

Try this:

dataSource: this.state.dataSource.cloneWithRows(JSON.parse(response.getB‌​odyString()))

Note: I am assuming getBodyString actually returns something because I don't recall that method existing in the response interface.

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

3 Comments

this didnt work. btw i am using react-native-netclient library for making api calls. getBodyString() returns me the json which i have commented on my question
It worked this way. Thank you buddy.. please update the answer . dataSource: this.state.dataSource.cloneWithRows(JSON.parse(response.getBodyString()))
Yeah, my bad. I actually meant to write that. Glad it worked!

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.