Hi I am learning React
Please go through my code, How do I set the state of a get response in axios?
const [state,setState] = useState({
bookingId: '', // This will contain booking ID from Input Field
bookingData: null, // This will contain booking data received from axios
infoMessage: ''
})
const onSubmit = (event) => {
event.preventDefault();
axios.get(url+state.bookingId).then((response.data)=>{
console.log(response.data);
setState({bookingData: response.data});
setState({infoMessage: ''});
console.log(state)
}).then((error)=>{
setState({bookingData: ''});
setState({infoMessage: `${state.bookingId} Not Found`});
})
})
<tr>
<td>{state.bookingData.id}</td>
<td>{state.bookingData.emailId}</td>
</tr>
I referred this thread How to set state of response from axios in react
But I couldn’t find a solution, still I am unable to get the response data inside state value
2
Answers
Lets check the code,
response.data
useresponse
as the parameter in the.then
callback.catch
for error handling instead of chaining another.then
.For setting the state, you can do it something like
Do a quick google search to see how the above block works, or refer the link posted by Antonija
You can set State value in React by this two ways: