score:3
Accepted answer
don't use async/await
and .then()
together. use either of those.
const login = async () => {
const response = await axios.get('/api');
const parseddata = json.parse(response.data.resp);
const userdata = parseddata.data;
setloginresponse({
token: userdata.token,
userid: userdata.user.id,
username: userdata.user.username,
firstname: userdata.user.firstname,
rccode: userdata.user.attributes.rccode
});
}
score:0
in the .then
setloginresponse({...loginresponse, token: response.data.resp.data.token, userid: response.data.resp.data.user.id, ...}
you can destructure your response object first and store values in variables to make the setloginresponse more easy to read.
Source: stackoverflow.com
Related Query
- populate hooks with axios json response reactjs
- Populate input fields from axios response reactjs
- React hooks not being set properly with JSON API response
- Prevent duplicates from json array with reactjs and axios
- Fill a reactjs map with json objects from request response inside for loop
- Dropdown from JSON response in React with hooks
- How to use map on json response returned by a REST API with ReactJs
- Populate React dropdown with Axios response
- ReactJS axios POST request send all options as a single JSON key with empty value
- How to POST a JSON with data provided by user with REACTJS and Axios
- How to setState response of API request in ReactJS with Hooks
- axios post request with json data
- Multiple fetch data axios with React Hooks
- How do I fix a network error received from axios response reactjs
- Axios request with chunked response stream from Node
- reactjs make https (not http) requests with axios
- Catch return json from POST with Axios when error 400 (Bad Request) occurs
- How to display json data with ReactJs as table
- Axios Post Form with Reactjs
- How to solve TypeScript error with axios response in React app
- Axios auto refresh every 60 seconds with Reactjs
- Get axios Response object when error occurred with Redux Saga
- Upload Image and indicating response in a table with axios and JavaScript
- How to see axios error response JSON in React
- Making 2 sequential requests with Axios - second request depends on the response of 1st
- Call web api method inside ReactJS web worker with axios
- ReactJS How do I use state hooks for a list of text changing buttons with a timeout?
- reactjs axios get request with custom header
- How to populate select dropdown elements with data from API - ReactJS
- How to paginate Cloud FireStore data with ReactJs and Hooks
More Query from same tag
- List always returns undefined
- Owl Carousel with React
- React Invalid left-hand side in assignment when checking data values
- text is covering the icons in input field reactjs
- How can i render a route component in the parent component from a child component
- CORS 403 on React JS
- How do I create an array of unique field values in React with data from Firestore?
- Reactjs input multiplication
- How to stretch width of a Dialog box of material ui in reactjs?
- useState doesn't update the value of my variable
- calling data from axios in react causes error
- How to use i18next to load resource from PHP?
- Problem on Dropdown Broken layout in ReactJS
- How can I realize "find" with using "$in" by two parameters in mangoose?
- Redux pass up/refresh state from current page
- Effect is not run (not even once) when state setter is called in function body
- How to set state based on document.body but before render (react)
- Why does the wrapper div only take some part of the page when it has all the components in it?
- Update style of one Element from mouseOver on other Element React
- Hide react bootstrap toast when user clicks outside
- Make a grid system in React which changes the number of rows and columns?
- How to push some HTML every 3 times to array for output
- (ReactJS) Getting error in Login Component, while Register Compo. works perfectly
- HTTP Error 400 running ASP.NET Core 2.1 React application
- Sending props to a React component for a Jest unit test
- Typescript runtime module loading (with webpack) and dll-like late binding
- Double rendering in React with asynchronous call in componentDidMount causing error
- ReactJS rendering image from array
- React Native Debugger state undefined
- How to combine onChange event for basic Validation Form in reactjs