Is the array undefined because the component is rendering before the data is loaded?


Charan

I have imported a set of objects from a separate js file into my main app.js file. I want to map through an array and pass object values ​​as props to separate components. When I run this code, the students array is returned as undefined.

I'm new to React and js, it's a learning project where I'm working with functional components.

import React, { useState, useEffect } from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import * as serviceWorker from './serviceWorker';
import { pupils } from "./pupils"
import PupilCard from "./Components/PupilCard";


const App = () => {



 return (<div>
    {pupils.map(pupil => 
    <PupilCard 
        firstname={pupil.firstname} 
        surname={pupil.surname}
        photo={pupil.photo}
        behaviourPoints={pupil.behaviourPoints}
        assessmentOne={pupil.assessmentOne}
        assessmentTwo={pupil.assessmentTwo}
        assessmentThree={pupil.assessmentThree}  
     />)}

 </div>)}

ReactDOM.render(<App />, document.getElementById('root'));

serviceWorker.unregister();


student.js example

const pupils = [
    {
        firstname: "Name",
        surname: "Name",
        photo:"./assets/pupilPhotos/user.png",
        behaviourPoints: 25,
        assessmentOne:70,
        assessmentTwo:60,
        assessmentThree:80        
    },
 {
        firstname: "Name",
        surname: "Name",
        photo:"./assets/pupilPhotos/user.png",
        behaviourPoints: 25,
        assessmentOne:70,
        assessmentTwo:60,
        assessmentThree:80        
    }]
Andrew Mecheriakov

To use "student" in this way, it has to be exported as export const pupils = .... Maybe you export it like that should import it export default...without breaking this cheat sheet import pupils from './pupils' can help you understand import/export better

Related


Rendering happens before data is loaded - React Redux

Swayze I can get the data from the backend api, but my page renders before the data is entered. How to wait for data and render page with incoming data from api? I am displaying data on Mui datatable. The code below shows my efforts. Thanks in advance. How can

Rendering happens before data is loaded - React Redux

Swayze I can get the data from the backend api, but my page renders before the data is entered. How to wait for data and render page with incoming data from api? I am displaying data on Mui datatable. The code below shows my efforts. Thanks in advance. How can

Rendering happens before data is loaded - React Redux

Swayze I can get the data from the backend api, but my page renders before the data is entered. How to wait for data and render page with incoming data from api? I am displaying data on Mui datatable. The code below shows my efforts. Thanks in advance. How can

Make sure the Vuex state is loaded before rendering the component

let us. I have a add-user.vuecomponent. This is the template I use to add new users and edit existing users. So on page load I check if the route has idand if it is , load the user from the state array to edit it. My problem is userundefined because the state

Make sure the Vuex state is loaded before rendering the component

let us. I have a add-user.vuecomponent. This is the template I use to add new users and edit existing users. So on page load I check if the route has idand if so load the user from the state array to edit it. My problem is userundefined because the state array

Make sure the Vuex state is loaded before rendering the component

let us. I have a add-user.vuecomponent. This is the template I use to add new users and edit existing users. So on page load I check if the route has idand if so load the user from the state array to edit it. My problem is userundefined because the state array

Get data from api before rendering component

Alex I send 2 api requests before rendering the page: const Profile = { template: '#profile', attributes: null, photos: [], data: function () { return {attributes: Profile.attributes, photos: Profile.photos}; }, beforeRouteEnter

ReactJS - load data before rendering component

Adel In ReactJS I change the route to a new route with "id" and then based on this "id" I have to call the API and get the data. I used an API call componentDidMountand componentWillMounttried setState()to keep the data in that state. But in my case they don't

How to stop component rendering before getting data?

kill Currently, in React, I have to use a function in the componentDidMount lifecycle method, where the action creator is called to fetch the data into it. However, the component will use the data, depending on the speed of the response, to determine if the da

Get a component to wait for async data before rendering

Countless Bruts I'm calling an endpoint to bring back an object that does get the data, but the object isn't fast enough for the component to get the data and render. Instead, the component will render with blank values where there should be data. If I break t

Set the data fetched by ajax to the component before rendering

Slabo I use Vue Router with file based components. I call one of the routes CaseDetail.vueand it gets a parameter (slug) which gets json from an API with axios. I'm doing this using " in-component guarding " , the method is called beforeRouteEnter. I'm passing

Get data from api before rendering component

Alex I send 2 api requests before rendering the page: const Profile = { template: '#profile', attributes: null, photos: [], data: function () { return {attributes: Profile.attributes, photos: Profile.photos}; }, beforeRouteEnter

Get data from api before rendering component

Alex I send 2 api requests before rendering the page: const Profile = { template: '#profile', attributes: null, photos: [], data: function () { return {attributes: Profile.attributes, photos: Profile.photos}; }, beforeRouteEnter

How to stop component rendering before getting data?

kill Currently, in React, I have to use a function in the componentDidMount lifecycle method, where the action creator is called to fetch the data into it. However, the component will use the data, depending on the speed of the response, to determine if the da

ReactJS - load data before rendering component

Adel In ReactJS I change the route to a new route with "id" and then based on this "id" I have to call the API and get the data. I used an API call componentDidMountand componentWillMounttried setState()to keep the data in that state. But in my case they don't

How to stop component rendering before getting data?

kill Currently, in React, I have to use a function in the componentDidMount lifecycle method, where the action creator is called to fetch the data into it. However, the component will use the data, depending on the speed of the response, to determine if the da

ReactJS - load data before rendering component

Adel In ReactJS I change the route to a new route with "id" and then based on this "id" I have to call the API and get the data. I used an API call componentDidMountand componentWillMounttried setState()to keep the data in that state. But in my case they don't

Get a component to wait for async data before rendering

Countless Bruts I'm calling an endpoint to bring back an object that does get the data, but the object isn't fast enough for the component to get the data and render. Instead, the component will render with blank values where there should be data. If I break t

Finish rendering component before fetching data

Faris I am trying to get data by creating a function. In that function, I'm trying to set the state, and call the state from the componentDidMountmethod , but I'm having some problems: I'm not sure if it's whilegood practice because I'm looping and changing en

Get data from api before rendering component

Alex I send 2 api requests before rendering the page: const Profile = { template: '#profile', attributes: null, photos: [], data: function () { return {attributes: Profile.attributes, photos: Profile.photos}; }, beforeRouteEnter

ReactJS - load data before rendering component

Adel In ReactJS I change the route to a new route with "id" and then based on this "id" I have to call the API and get the data. I used an API call componentDidMountand componentWillMounttried setState()to keep the data in that state. But in my case they don't

Get a component to wait for async data before rendering

Countless Bruts I'm calling an endpoint to bring back an object that does get the data, but the object is not fast enough for the component to get the data and render. Instead, the component will render with blank values where there should be data. If I break

Get data from api before rendering component

Alex I send 2 api requests before rendering the page: const Profile = { template: '#profile', attributes: null, photos: [], data: function () { return {attributes: Profile.attributes, photos: Profile.photos}; }, beforeRouteEnter

ReactJS - load data before rendering component

Adel In ReactJS I change the route to a new route with "id" and then based on this "id" I have to call the API and get the data. I used an API call componentDidMountand componentWillMounttried setState()to keep the data in that state. But in my case they don't