So I have this Display()
function which fetches events from the Google Calendar via an API and store each event's name via element.summary
into the events
set. And then once the events
set is populated, I iterate through the set via for (let item of events)
and create a new <a>
tag for each event/item in the set using the name as the text via <a>{item}</a>
(for e.g. <a>call<a>
, then I push each <a>
tag into a new array called tabs
and then finally return the tabs
array. The events
set contains three items and when I console.log
, I see the correct items ("call", "kist", & "go") in the set. However, once I console.log
the tabs
array, it only contains one <a>
tag whose value is null whereas it is supposed to contain three <a>
tags since it iterates through the events
set which has three items and is supposed to create an <a>
tag for each. Also, I get the error that item is not defined
for the line for (let item of events)
, somehow I cannot iterate through the events
set. See console output here.
function Display() {
let events = new Set()
let tabs = []
ApiCalendar.listUpcomingEvents(10)
.then(({result}: any) => {
result.items.forEach(element => {
console.log(element.summary)
events.add(element.summary)
}
);
console.log(events)
for (let item of events)
console.log(item)
tabs.push(<a>{item}</a>)
console.log(tabs)
return tabs
});
}
This is the class that I made in the same file as the above function, which basically renders a 'Log In' button if user is not logged in to their calendar, or renders the array of <a>
tags returned by the Display()
function if user is already logged in. However, even though the Display()
function above does return something (i.e. an array of <a>
tags) and the render()
function inside the class also returns a <div>
element with the corresponding component inside the div
, I get the error Uncaught Error: Display(...): Nothing was returned from render. This usually means a return statement is missing. Or, to render nothing, return null.
I am new to JavaScript and have no idea what I'm doing wrong. Any help is greatly appreciated and thank you in advance.
export default class LoginControl extends React.Component {
constructor(props) {
super(props);
this.state = {
sign: ApiCalendar.sign,
};
}
render() {
const isLoggedIn = this.state.sign;
let ele;
if (isLoggedIn) {
ele = <Display/>;
} else {
ele = <Button>'Sign In'</Button>;
}
return (
<div>
{ele}
</div>
);
}
}