score:2
Accepted answer
in getuserprofile()
you are returning this.state
before your axios get request is complete. when you call getuserprofile()
from page.js you are trying to access the data before it is set from your get request.
you should be returning the promise in getuserprofile()
then using async/await or .then()
when you call it in componentwillmount()
before setting the data
getuserprofile(){
const url = "https://somewebsite.com/api/user_profile/"
const user_token = localstorage.getitem('access_token')
const authstr = 'jwt '.concat(user_token);
return axios.get(url, { headers: { authorization: authstr } })
};
async/await
async componentwillmount() {
const userdatares = await (new vapi().getuserprofile());
const userdata = userdatares.data;
this.setstate({
first_name: userdata.first_name,
last_name: userdata.last_name,
email: userdata.email,
company: userdata.company,
});
console.log('state', this.state);
}
promise
componentwillmount() {
new vapi().getuserprofile().then((userdatares) => {
const userdata = userdatares.data;
this.setstate({
first_name: userdata.first_name,
last_name: userdata.last_name,
email: userdata.email,
company: userdata.company,
});
console.log('state', this.state);
});
}
Source: stackoverflow.com
Related Query
- Dynamic configuration variables in Javascript / React
- React / Javascript Variables
- How to access Sass variables using javascript inside a react component?
- Access global javascript variables using react
- How can I change the scroll direction in React js using javascript and get the scroll variables
- Concatenating variables and strings in React
- react evironment variables .env return undefined
- How to define css variables in style attribute in React and typescript
- React Hooks - using useState vs just variables
- Create React App: using environment variables in index.html
- Question mark before dot in javascript / react
- What does React Native use to allow JavaScript to be executed on iOS and Android natively?
- JavaScript and ES6, "global" variables
- react native downloading javascript bundle 100% stuck
- Writing embeddable Javascript plugin with React & Webpack
- Call external Javascript function from react components
- React Javascript displaying/decoding unicode characters
- Set React Input Field Value from JavaScript or JQuery
- Embeddable JavaScript widget with React
- How to use environment variables in React app hosted in Azure
- How to use SCSS variables into my React components
- Import third-party JavaScript library that normally self-executes into React app (using create-react-app)
- What's the difference between a JavaScript function and a React hook?
- Show the name of state variables from useState in React Developer Tool
- Modify React Component's State using jQuery/Plain Javascript from Chrome Extension
- You need to enable javascript to run this app response when try to access local json file in react
- Can React Helmet inject a javascript object into <HEAD> tag?
- Using a regular javascript library inside a React component
- How does JavaScript mechanism behind react hooks work?
- React hooks: How to write variables in functional components that in class components were initialized in the constructor
More Query from same tag
- abort state set with React useState hook function call
- Server Side React Initial Render causes duplicated async calls
- Bootstrap Modal renders in React Component but is not visible
- How do I add user input inside an object array with useState?
- React setInterval inside useEffect
- How to pass an array of objects as a prop in react?
- How to mock a method which is part of a node-module using jest?
- Why can we update a value of a number input using mouse wheel?
- How to filter data based on selected value from dropdown menu and search query in input field using reactjs?
- How to access map function state and use in external function
- How to use react to implement google Place's Searches, to create markers on the map?
- use react in views folder in mvc express project
- Standard directory structure for a React project (or component library)?
- React Redux not having context
- Webpack unable to find babel-loader
- React does not re-render updated array state
- Dynamically add columns for csv data JS
- Onclick function for multiple buttons with same ID?
- How to pass Array from Parent component to Child component in reactjs
- Javascript Map and Filter on two array
- Why IndexOf(Object) in the redux case of React App is work?
- using renderRichText - receiving an error Node is not defined
- How to map images in reactjs
- my check box in react app is not changing state after clicking the check box
- changing state of individual react component
- react-grid-layout with typescript not working
- How to create session in nodejs + react
- Floating label over-aligning the input text
- In ReactJs is it possible/feasible to perform rendering during a Drag/Drop operation?
- Reactjs client does not get cookie from Express server