0

I have an array in JSON:

[
    {
        "id": 0,
        "type": "phone",
        "model": "Iphone 11"
    },
    {
        "id": 1,
        "type": "phone",
        "model": "Iphone 11 PRO"
    }
]

I have an 'axios' request :

const [phone,setPhone] = useState({}) // react-hook

const getPhones = () => {
      Axios.get('/phones.json').then(({data}) => {
      setPhone(data)
      console.log(data)
    })
  }

So I want to show, for example 'type' and 'model' from 1st and 2d objects? How i can do it?

It doesn't work

    <div>
      <div>{phone.type}</div>
      <button onClick={getPhones}>click</button>
    </div>
2
  • 1
    Try looping the array with array#map. Commented May 3, 2020 at 17:59
  • Does this answer your question? Loop inside React JSX Commented May 3, 2020 at 18:04

1 Answer 1

1

Try to loop and print out. like this:

<Table> <!-- or dl dd structure -->
<tr>
  <th>c1</th>
  ...
</tr>
{
   phone.map((p,index)=>(
      <tr key={index}>
        <td>{p.mtype}</td>
        ...
      </tr>

   )
}
</Table>
Sign up to request clarification or add additional context in comments.

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.