View data from React Native API

Asked

Viewed 286 times

-1

Hi Dev’s I’m new in RN, I’m trying to show the return of a json, but I can’t access by the handle on the screen, only in the console.

export default Function Functional() { const [date, setData] = useState([]); const [Refreshing, setRefreshing] = useState(true);

useEffect(() => { async Function loadInitial() {

  let id = 'XXX';
  var retorno = await getUser().then((value) => {
    let token = JSON.parse(value);
    console.log(token)
    return token
    }, (error) => {
    console.log(error) 
  });

  const response = await api.get('users/'+id,{ params: { jwt: retorno.jwt } })

  console.log(response.data)

  setData(JSON.stringify(response.data));
  
  //setData(JSON.stringify(response.data.data)); Tentei assim tambem
}

loadInicial();

}, []);

// That way It doesn’t work Return( {date.name}

),

//This shows the whole Array Return( {date}

),

//JSON that is returning {"date": {"name": "José Paulo"}}

On the console I can print the key smoothly

  • I got Galera Tava Vacilando, Remove the Stringify() and it worked.

1 answer

0

Maybe the problem is that you are converting using JSON.stringify() and passing to the state.

Just try to pass to the state sponse.date. without converting it.

Example:

setData(response.data);
  • Perfect, it worked. Obg!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.