0

I am sending a GET request on a Node API with a MongoDB server. I am getting the response as JSON in an array of object format. I want to show all those results in a list. Right now i am making a function like this

class VendorDashboard extends React.Component {
    constructor() {
      super();

      this.state = {

        paginationValue: '86',
        title: ""
      }

      this.handleLogout = this.handleLogout.bind(this);
      this.gotoCourse = this.gotoCourse.bind(this);
    }
    componentDidMount() {
        axios.get('/vendor/showcourses') //the api to hit request
          .then((response) => {
            console.log(response);
            let course = [];
            course = response.data.map((courseres) => {
              this.setState({
                title: courseres.title
              });
            })
          });

Right now what is happening is it is showing just one result. I want to show all results on that api. How can i do it?

1
  • 3
    you are overriding your title state in each iteration.That's why it is showing only last result Commented Mar 29, 2018 at 7:52

3 Answers 3

3

This segment here is overriding the title per course.

course = response.data.map((courseres) => {
          this.setState({
            title: courseres.title
          });
        })

You can keep the state as an array of titles and do;

course = response.data.map((courseres) => {
          return courseres.title;
        })

this.setState({titles: course});

And then you can repeat on the array of titles in your component. Like so in the render method;

const { titles } = this.state;
return <div>{titles.map((title, index) => <div key={index}>{title}</div>)}</div>
Sign up to request clarification or add additional context in comments.

Comments

2

You need to collect all the server response and set that as an array of data to the state and use this state data to render:

class VendorDashboard extends React.Component {
    constructor() {
        super();

        this.state = {
            paginationValue: '86',
            course: []
        }

        this.handleLogout = this.handleLogout.bind(this);
        this.gotoCourse = this.gotoCourse.bind(this);
    }

    componentDidMount() {
        axios.get('/vendor/showcourses') //the api to hit request
            .then((response) => {
                const course = response.data.map((courseres) => ({
                    id: courseres.id,
                    title: courseres.title
                }));

                this.setState({
                    course
                });
            });
    }

    render() {
        return (
            <ul>
                {
                    this.state.course.map((eachCourse) => {
                        return <li key={eachCourse.id}>{eachCourse.title}</li>
                    })
                }
            </ul>
        )
    }
}

Comments

0

In each map iteration you rewrite your piece of state, it is wrong. Just put courses in your state:

        console.log(response);
        this.setState({ courses: response.data });

In render method go through your state.courses:

render(){
  return(
   <div>
    {this.state.courses.map(course => <h2>{course.title}</h2>)}
   </div>
  );
}

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.