0

I'm trying to render an object with an array into an array (a JSON), that I receive from an API. I've tried a lot but I don't get with the solution.

This is the object format I receive:

{
    "catalogue": [
        [
            "shoes.txt",
            "trousers.txt",
            "tshirts.txt"
        ]
    ]
}

This is the code I'm using to call the API and render the object:

import React, { Component } from "react";
import axios from "axios";

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

  componentDidMount() {
    const URL = "http://localhost/catalogue";
    fetch(URL)
      .then((response) => response.json())
      .then((data) => this.setState({ items: data }));
  }

  render() {
    const catalogue = this.state.items.catalogue;
    return <div>{catalogue}</div>;
  }
}

export default Result;

The only I could show in the web is this:

shoes.txttrousers.txttshirts.txt

The view I can get is something like this:

shoes.txt
trousers.txt
tshirts.txt

Would somebody help me please? Thanks in advance.

3 Answers 3

1

Try this:

import React, { Component } from "react";

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

  componentDidMount() {
    const URL = "http://localhost/catalogue";
    fetch(URL)
      .then((response) => response.json())
      .then((data) => this.setState({ items: data.catalogue[0] }));
  }


  render() {
    let catalogue = this.state.items.map((item, key) => (
      <li key={key}>{item}</li>
    ));
    return <ul>{catalogue}</ul>;
  }
}

export default Result;

Pay attention to the "[0]" in this line:

.then((data) => this.setState({ items: data.catalogue[0] }));

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

5 Comments

It works but it gives me the same output I get, with all the values together in the same line.
Same line ? Its div which will show each item in new line. However, try this: render() { let catalogue = this.state.items.map(item => ( <li key={item}>{item}</li> ) return <ul>{catalogue}</ul>; } }
With that I get all in the same line but with a dot at the beginning. Maybe there is a problem with the object format?
Piyush: with the code you wrote here using <li> and <ul> tags and adding "[0]" into this.setState function (for the array into array), it works! I've edited the code you paste here to mark as working answer. Thanks!
Oh yes, I missed it. The catalogue contains an array at its 0 index. Thanks 4 correcting.
0

I'm not 100% sure this is what you're looking for, but this should set you on the right track to achieving what you want eventually

Change

return <div>{catalogue}</div>;

to

return (
  <div>
    {catalogue.map(item => (
      <div key={item}>{item}</div>
    )}
  </div>
);

This will render your each entry in your array as a separate

6 Comments

If you dont add key you get warning in console
I tried that but I get this: TypeError: Cannot read property 'map' of undefined
Did you include the line above the return statement inside your render body? i.e. const catalogue = this.state.items.catalogue;
Yes, I did, exactly like this: render() { const catalogue = this.state.items.catalogue; return ( <div> {catalogue.map((item) => ( <div key={item}>{item}</div> ))} </div> ); }
You will get TypeError, at first render. Initiallly, react is trying to get this.state.items.catalogue which is not there as items is empty array here. Thus, error app stuck here. I have added my answer you can try that.
|
0

Just change the render method to

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

  componentDidMount() {
    const URL = "http://localhost/catalogue";
    fetch(URL)
      .then((response) => response.json())
      .then((data) => this.setState({ items: data.catalogue }));
  }


  render() {
return(
       <div>
      {this.state.items.catalogue.map((item, index) => (
        <span key={index}>{item}</span>
      ))}
    </div>
)}

3 Comments

Same error that in previous answer: TypeError: Cannot read property 'map' of undefined
Edited my answer should work now. Also, use the hooks they are very powerful and easy.
It works but it gives me the same output I've got, with all the values together in the same line, as with other answers.

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.