Home>

The simplest React.js code doesn't work for me I would really like to understand what exactly is wrong, any help is helpful. Thanks a lot in advance)

Code:

import './style/App.css';
import React, {useState, useEffect} from 'react';
function App() {
    const[prisvoenie, setPrisvoenie]= useState({});
    useEffect(()=> {
        fetch('https://jsonplaceholder.typicode.com/users')
        .then((response)=> response.json())
        .then((res)=> {
          setPrisvoenie(res);
        })
  },[])
  return (
    <div className="App">      {private}
    </div>  );
}
export defaultApp;
  • Answer # 1

    The variable prisvoenie is an object (it will become an array after the request), and react does not allow objects to be rendered, you need to do prisvoenie.map() and return the markup from it

    import './style/App.css';
    import React, {useState, useEffect} from 'react';
    function App() {
        const[prisvoenie, setPrisvoenie]= useState({});
        useEffect(()=> {
            fetch('https://jsonplaceholder.typicode.com/users')
            .then((response)=> response.json())
            .then((res)=> {
              setPrisvoenie(res);
            })
      },[])
      return (
        <div className="App">      {prisvoenie.map(el=> {
            return <here is your code>      })}
        </div>  );
    }
    export defaultApp;