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>

1 Answers1

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>
Vahid Alimohamadi
  • 4,900
  • 2
  • 22
  • 37