Working with a react component using hooks. I am attempting to assign values to my state after an Axios call. The data is correctly returned and it appears the state is correctly updated; however, the DOM is not updating with the values. Is this the proper method for doing this?
export const Main = (props) => { const { token } = useParams() const [user,setUser] = useState({name:""}) useEffect(()=>{ const getUser = async() =>{ const response = await API.post("usrinfo",{token:token}) setUser({name:response.data.name}); } getUser() },[token]) return( <div className="main-header">{user.name}</div> ); }
Found the issue:
AXIOS returns a data portion in it's response so instead of response.data.name it should be response.data.data.name.
response.data.name
response.data.data.name
Thanks for all your responses.
2.1m questions
2.1m answers
60 comments
57.0k users