score:0

setcar is asynchronous so you can't get cars immediately after it is set, which is why you get undefined in the console.log.

however, cars are being set correctly to state, but because it's a list you can only map through it to be able to access the values.

consider refactoring the useeffect to setcars only when data is available

useeffect(() => {
    const fetchposts = async () => {
      const res = await axios.get("http://192.168.29.135:8000/data/cars");
      if (res.data) {
        setcar(res.data);
      }
    };
    fetchposts();
  }, []);

so your return should return jsx like this

return (
      <div>
            {cars.map(car => 
                <span key={car.id}>
                  {car.car_name}
                </span>
            )}
       </div>
);

Related Query

More Query from same tag