Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Able to log array in console, but can't loop through it

Running into a problem. In react I’m grabbing data from a json api. Everything worked fine until I tried to display it. Except, I’m able to log it on the console.

What the console logs:

pic

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<ul role="list" className="divide-y divide-gray-200 dark:divide-gray-700">
{username !== "null" && leaderlist.length > 2 && console.log(leaderlist) && leaderlist.map((leader, index) =>
       <li className="py-3 sm:py-4 text-white">
           Log 
        </li>
      )}
     <li className="py-3 sm:py-4 text-white">
         list length {leaderlist.length} 
     </li>
   </ul>

What it displays:

enter image description here

The code that sets

const [leaderlist, setleaderlist] = React.useState([]);
fetch(`${baseurl}/allusers`)
     .then(res => res.json())
     .then(data => {setleaderlist(data); console.log(data)})
     .catch(err => console.log(err));

>Solution :

username !== "null" && leaderlist.length > 2 && console.log(leaderlist) && leaderlist.map((leader, index)

console.log(leaderlist) does not have a return value and thus leaderlist.map((leader, index) ...) does not run since you’re using &&.
You need to remove the console.log statement or use ; instead of &&.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading