0

I have been trying to get data from the api using axios get request but it keeps on giving me error. The error is TypeError: this.setstate is not a function i have seen multiple queries about it and almost all of them had done it this way but i cant figure out what iam doing wrong.

My code for axios get

import React, { Component, Fragment } from 'react';
import axios from 'axios'

class Questionare extends Component {
    constructor(props) {
        super(props);
        this.state = {  
          items: [],

    }
} 

componentDidMount() {
  axios.get('http://leaty-dev.azurewebsites.net/api/services/app/PersonalityQuestionerCategory/GetAllPersonalityQuestionerCategory')
  .then(response => {
      console.log(response);
      this.setstate({items: response.data})
  })
  .catch(error => {
    console.log(error);
  });
}

    render() { 

       return (
          <ul>
            {this.state.items.map(item=>(
             <li>{item.pqcDetail}</li>
            ))}
          </ul>
       )
    }

}

export default Questionare;
2
  • I suggest converting response to json by response.json() Commented Jan 10, 2020 at 6:20
  • My mistke has been corrected it was camel case mistake but i have another error TypeError: this.state.items.map is not a function Commented Jan 10, 2020 at 6:24

1 Answer 1

2

It's camel case setState() not setstate()

corrected

this.setState({items: response.data})

Improved complete code ---Live https://74u3w.csb.app/

import React, { Component } from "react";
import ReactDOM from "react-dom";
import axios from "axios";

class Questionare extends Component {
  state = {
      items: []
    };
  componentDidMount() {
    axios
      .get(
        "https://cors-anywhere.herokuapp.com/http://leaty-dev.azurewebsites.net/api/services/app/PersonalityQuestionerCategory/GetAllPersonalityQuestionerCategory"
      )
      .then(response => {
        this.setState({ items: response.data.result.items });
      })
      .catch(error => {
        console.log(error);
      });
  }

  render() {
    const { items } = this.state;
    return (
      <ul>
        {items.map(item => (
          <li>{item.pqcDetail}</li>
        ))}
      </ul>
    );
  }
}
Sign up to request clarification or add additional context in comments.

8 Comments

Thank you so much it worked but i have another problem
TypeError: this.state.items.map is not a function
@AbdulRauf can you show your json response data sample?. is it array or object
its an array of 7 items
Thank you so much for your help. @VahidAkhtar
|

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.