I'm trying to pass an array of objects to my MenuPage from the App.js. If there's no map, I can consolelog the activities prop I passed. But when I try to run the map I get an error and the console.log can no longer identify the activities prop, but instead I receive multiple console.logs of empty arrays.
Also, is this the correct way so that my activities render dynamically? I used to have the activities state on the MenuPage.js, but whenever I add a new activity, I'd have to refresh in order for the newly added activity to appear. Hence, I decided to try to move it in its parent file which is the App.js.
error message when map
Error: Too many re-renders. React limits the number of renders to prevent an infinite loop.
Console log of activities prop if no map
{getActivities: Array(5)}
getActivities: Array(5)
0: {id: "5e64abc6de157e027c1fc224", name: "Test1", location: "Test1", activityDate: "Test1", description: "Test1", …}
1: {id: "5e69b103dd9e0b20f8636da9", name: "Test234", location: "Test2", activityDate: "Test2", description: "Test2", …}
2: {id: "5e6a744eca11a3355cdde03b", name: "test3", location: "test3", activityDate: "test3", description: "test3", …}
3: {id: "5e6a74c9be805e042057b3b1", name: "test4", location: "test4", activityDate: "test4", description: "test4", …}
4: {id: "5e6a74e4be805e042057b3b2", name: "test5", location: "test5", activityDate: "test5", description: "test5", …}
length: 5
__proto__: Array(0)
MenuPage.js
const MenuPage = ({activities}) => {
const [activity, setActivity] = useState([])
console.log(activities)
setActivity(activities.map( (activity) => <ActivityItem activity={activity} />))
App.js
const App = () => {
const [activities, setActivities] = useState([])
useEffect( () => {
GQLClient({}).request(Query.getActivities, null).then( ({getActivities}) => {
setActivities({getActivities})
})
}, [])
const Load = (props, page) => {
if (user.token === null) return <Redirect to="/login" />
if (page === 'LogoutPage') {
unsetUser()
return <Redirect to="/login" />
}
switch (page) {
case 'MenuPage': return <MenuPage activities={activities} />
default:
}
}
return (
<BrowserRouter>
<AppNavbar />
<Switch>
<Route exact path='/register' component={ RegisterPage } />
<Route exact path='/login' component={ LoginPage } />
<Route exact path='/' render={ (props) => Load(props, 'MenuPage') } />
</Switch>
</BrowserRouter>
)
}
return <>{activities.map(a=>ActivityItem activity={a} key={a.id}/>}</>