I am fetching some data with react from django rest framework and put them into state. And then pass them into another component with props. When I print them I get the object and it works perfectly. For example console.log(props.user1)
gets me this:
Array(1)
0:
{
date_joined: 2023-09-06T16:07:37.214335Z"
email: ""
username: "t"
}
...
But when I print props.user1.username
I get
TypeError: Cannot read properties of undefined (reading 'username')
2
Answers
Well the problem was way more simple.
I had defined state like this:
const [user,setUser] = useState()
instead of this
const [user,setUser] = useState({})
it is look like:
0: { date_joined: date, email: String username: String }
if you want to get username, you have to use
props.user1.0.username
you can’t use
props.user1[0]