-1
I’ve had this problem for weeks:
I am using React Hooks to request an internal api using useEffect to make a request and then store the result within a setState. If I give a console.log the first result and array [ ] (empty) and then an array with the expected information, when I try to render inside a<Text>{user.name}</Text>
gives error below.
Typeerror: Typeerror: null is not an Object (evaluating 'user.name')
const [user, setUser] = useState([])
...
useEffect(() => {
async function loadUser(){
const response = await api.get('/user/painel', {
headers: {user_id}})
setUser(response.data)
}
loadUser();
}, [ ])
...
return(
<View style={styles.container}>
<Text>{user.name}</Text>
</View>
)
That’s right, the function returns an array and within an object. I was picking up the object, so I couldn’t render {user.name}. The solution was to make a user.map() to render, but as it comes to React Native I used <Flatlist>. Thanks!!!
– Mateus Cazuza
If you have this user’s ID, vc tbm can use the Array.find( ) method to find this user within the array. Ai later you can use it with ' user.name }.
– Jonathan Souza