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>
);
Source: stackoverflow.com
Related Query
- Extracting data from GET request using axios
- How to get data from my axios request without using a setTimeout?
- How do I access data returned from an axios get to display on a web page using react js?
- Accessing data from Axios GET request
- browser says " request has been blocked by CORS policy" when calling to a spring boot get method from react js using axios
- React: Using Axios so I can use data from my first request in my second one
- How to access data from get request made by a React functional component to localhost server using Axios?
- The data I want to get from API using Axios returns undefined
- react js : axios fecth data using a get request condition : i want names with those values with taken: true
- While developing a voice agent, I am using axios to get data from a spreadsheet. However, I cannot get all data at once
- Update data from array with multiple objects using Axios PUT request
- For some reason, cant get the data from an api to render on the screen using axios in Reactjs
- React and Express: Using Axios to GET data from 3rd party API
- Get response data from Axios using redux thunk
- get data from server using axios with react
- using data from GET request in react and use in child component
- How to get Data from api using axios in redux-toolkit
- Getting response data from post request when using axios
- How do I sustain data from DB while using another GET request with different query string in React(Next.js)?
- Making an axios get request and using React useState but when logging the data it still shows null
- Can't get multiple data from API using foreach loop with Fetch API get request
- Get data from Axios request
- Trying to Print Data from Get Request via Axios
- How to send user data and its profile image using POST request from axios api to Django Rest Framework?
- How to extract data from axios GET request object in React?
- how to get data from localhost:9200 (elasticsearch) using axios in react
- Using react, axios and django, i'm trying to send data from my client side to my server but I can't get the data
- How to get data from api using axios
- How to concatenate URL and string, and get data from API at click a button using axios in REACT JS
- how to send data in GET request header using react and axios to Django rest framework?
More Query from same tag
- Using a react app without rendering a html piece
- How to create dynamic elements inside global tabs
- react dropzone and nodejs file upload
- How to add map markers stored in a JSON feed to an existing React Google Map?
- When trying to create new item in MERN stack, getting TypeError: this.props.meals.map is not a function
- Display element at the top inside display:flex container
- Error in PUT API request in reactJS fetch from frontend
- React click button event on list always returns reference to the last element
- React delay button event for 2 seconds
- JSON Parse failed from string of json that coming from Swift conversion
- Making a value of an object to null in a object list in javascript
- Firestore Security rules with payment form
- How To Call A JavaScript Function Returned From NodeJS in ReactJS JSX
- TypeError: Cannot read property 'data' of undefined ....src/pages/index.js in Gatsby
- How can i load an mp3 file to a react URL
- Dynamic React path Routing not matching url but matches on reload
- Navigation menu list (sub-menu)dropdown isn't working in reactjs
- Using a redux store with react-navigation
- Nested react component (Header) causing site to be unresponsive
- Store bearer token in header
- Getting React component to work with jQuery listener
- Toggle in React not Toggling all elements
- UseEffect does not load at the right time Reactjs
- Clear the api response array before another API call
- How to allow javascript compilation with typescript errors,
- API call using React Hooks
- Extracting styles in React + material-ui
- Switching elements' visibility
- PropType.shape({}) Failure Warning: Invalid Type
- How to test function with another function inside with Jest?