1

This is my first time using React and I am having an issue accessing a nested data object.

I want to access the commute value under each object. I can access the top level data like activities.length. But, when I attempt something like activities[0].commute I am unsuccessful.

My goal is to list all activities with their respective commute values.

Any help is incredibly appreciated. Thanks!

Here's the code:

import React, { useState, useEffect } from 'react';

function App() {
const [isLoading, setIsLoading] = useState(true)
const [activities, setActivities] = useState({})


let clientID = "****";
let clientSecret = "****";


const refreshToken = "****";
const callRefresh = `https://www.strava.com/oauth/token?client_id=${clientID}&client_secret=${clientSecret}&refresh_token=${refreshToken}&grant_type=refresh_token`


const callActivities = `https://www.strava.com/api/v3/athlete/activities?access_token=`

useEffect(() => {
fetch(callRefresh, {
  method: 'POST'
})


.then(res => res.json())
.then(result => getActivities(result.access_token))
}, [callRefresh])


function getActivities(access){
  fetch(callActivities + access)
  .then(res => res.json())
  .then(data => setActivities(data), setIsLoading(prev => !prev))
  .catch(e => console.log(e))
 }

function showActivities(){
if(isLoading) return <>LOADING</>
if(!isLoading) {
  console.log(activities)
 return activities.length
  }
}

return (
  <div className="App">
 {showActivities()}
</div>
   );
  }

 export default App;

Here's the data: enter image description here

2 Answers 2

1

If activities is a list, then the default value needs to be a list.

const [activities, setActivities] = useState([]);

Then simply map activities into a list of components.

function showActivities() {
  if(isLoading) return <>LOADING</>;

  return (activities || []).map(activity => {
    return <Text>{activity.commute}</Text>;
  });
}
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks, windowsill. Unfortunately, this does not work because 'Text' is undefined. ERROR src/App.js Line 40:13: 'Text' is not defined react/jsx-no-undef
What? Why would I know how you want to render your activity commute? Use your own rendering logic.
Try it with <>{activity.commute}</>. If there are any errors, paste the error here please.
Uncaught (in promise) TypeError: (activities || []).map is not a function
So, I am able to access the top level object of the data such as activities.length, but cannot get deeper in the data structure.
|
0

You will now have access to the nested data objects, listing all activities with their respective commute values.

import React, { useState, useEffect } from 'react';

function App() {
const [isLoading, setIsLoading] = useState(true)
const [activities, setActivities] = useState([]);


let clientID = "****";
let clientSecret = "****";


const refreshToken = "****";
const callRefresh = `https://www.strava.com/oauth/token?client_id=${clientID}&client_secret=${clientSecret}&refresh_token=${refreshToken}&grant_type=refresh_token`


const callActivities = `https://www.strava.com/api/v3/athlete/activities?access_token=`

useEffect(() => {
fetch(callRefresh, {
  method: 'POST'
})


.then(res => res.json())
.then(result => getActivities(result.access_token))
}, [callRefresh])


function getActivities(access){
  fetch(callActivities + access)
  .then(res => res.json())
  .then(data => setActivities(data), setIsLoading(prev => !prev))
  .catch(e => console.log(e))
 }

function showActivities() {
  if(isLoading) return <>LOADING</>;

  return (activities || []).map(activity => {
    return <Text>{activity.commute}</Text>;
  });
}

return (
  <div className="App">
 {showActivities()}
</div>
   );
  }

 export default App;

3 Comments

I deeply apologize because I know you put energy into responding. I made an error; this is for ReactJS, not React Native. I believe I may have selected the React Native tag.
Thanks, Johan. Unfortunately, this does not work because 'Text' is undefined. ERROR src/App.js Line 40:13: 'Text' is not defined react/jsx-no-undef
@Devvvvv I see. I'm sorry. I do not know how to help you. Can you rate my answer at least for commitment and goodwill? Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.