0

I have data showing up as following in my backend server

enter image description here

I want to show this data in my frontend react app.
I have tried the following so far-

import {useState, useEffect} from 'react';
import logo from './logo.svg';
import './App.css';

function App() {
  const [data,setData] = useState([])
  useEffect( () => {
    fetch("http://127.0.0.1:5000/lang")
    .then(res =>  res.json())
    .then( res => {setData(res)} )
    console.log(data.languages)
  }, [] )
  return (
    <div className="App">
    {
        data.languages.map((data,i) => {
      <header className="App-header">
        <img src={logo} className="App-logo" alt="logo" />
        <p>
          Edit <code>src/App.js</code> and save to reload.
        </p>
        <a
          className="App-link"
          href="https://reactjs.org"
          target="_blank"
          rel="noopener noreferrer"
        >
          {data.languages}
        </a>
      </header>
    })
    }
    </div>
  );
}

export default App; 

I am getting the following error upon making this request-

enter image description here

What could I be doing wrong?

tallpotato
  • 163
  • 1
  • 8

2 Answers2

0

You have initialized the data with array and then you are assigning object to it, which is causing error in your code. You can initialize the data with {languages: []} to fix this. Also, you are missing return in your array#map.

const { useState, useEffect } = React;

const result = { languages : [ {name: "Javascript"}, {name: "Python"}, {name: "Ruby"}]};
const apiResponse = () => new Promise(resolve => setTimeout(resolve(result), 1000));

const App = () => {
  const [data,setData] = useState({languages: []});
  
  useEffect(() => {
    apiResponse().then(res => setData(res));
  },[]);
  
  return (
    <div className="App">
    {
        data.languages.map(({name},i) => {
        return (
          <header className="App-header">
          <p>
            Edit <code>src/App.js</code> and save to reload.
          </p>
          <a className="App-link" href="https://reactjs.org" target="_blank" rel="noopener noreferrer" ><br />
            {name}
          </a>
          </header>
        )
    })
    }
    </div>
  );
 
}

ReactDOM.render(
  <App />,
  document.getElementById("react")
)
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.1/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.1/umd/react-dom.production.min.js"></script>
<div id="react"></div>
Hassan Imam
  • 21,956
  • 5
  • 41
  • 51
0

Map is array.prototype , so you need to check if data.lenguages is array , and your map function doesn't return anything.

this is returning one div for all elements in array

  array.map(() => <div></div>)

this is returning undefined :

  array.map(() => {<div><div>})  

and this is returning div for all elements i array:

  array.map(() => {return <div><div>}) 

also you need to return single element , wrap all of them in div or in wraper :

  array.map(() => {return ( <>
                        <div>1<div>
                         <div>2<div>
                        <div>3<div>
                        </>
                         )})   
Shomy
  • 31
  • 4