0

Hi I want to render this Advice on the screen but I could not do it I tried to map but that didn't helped please help me

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

export default function UsersData() {
  const [Users, setUsers] = useState([{
    "slip": {
      "id": 41,
      "advice": "Don't use Excel or Powerpoint documents for your basic word processing needs."
    }
  }])

  return(
    <>
      <h2> React Fetch API Example </h2>
        <ul>
          {/* Not sure what to write here */}
        </ul>
    </>
  )
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

I tried {Users.map(e=>{e.slip}) but it didn't work.

3
  • What exactly did you try in terms of your mapping logic? Commented Apr 23, 2022 at 8:18
  • I tried {Users.map(e=>{e.slip}) but that didn't worked Commented Apr 23, 2022 at 8:20
  • Yeah that won't work because it would try to render slip or each user. But slip is an object. Try my answer. And if that works for you please consider marking the answer as a solution( ✅ ) to close the loop. :) Commented Apr 23, 2022 at 8:22

4 Answers 4

1

Using map function you can print whole array and sample code below here.

<ul>
    {Users.map((element) => (
      <li key={element.slip.id}>{element.slip.advice}</li>
    ))}
  </ul>
Sign up to request clarification or add additional context in comments.

Comments

1

It should be as simple as writing a mapping function:

export default function UsersData() {
  const [Users, setUsers] = useState([
    {
      slip: {
        id: 41,
        advice:
          "Don't use Excel or Powerpoint documents for your basic word processing needs.",
      },
    },
  ]);

  return (
    <>
      <h2>React Fetch API Example</h2>
      <ul>
        {Users.map((user) => (
          <li key={user.slip.id}>{user.slip.advice}</li>
        ))}
      </ul>
    </>
  );
}

Here's a sample for your ref.

Comments

0

Use this:

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

export default function UsersData() {
  const [Users, setUsers] = useState([
    {

      "slip": {
      "id": 41,
      "advice": "Don't use Excel or Powerpoint documents for your basic word processing needs."
      }
      }
  ])



  return (
    <>
      <h2>React Fetch API Example</h2>
      <ul>
        {Users.map(({slip})=>(
           <li key={slip.id}>{slip.advice}</li>
        ))}
      
      </ul>
    </>
  )
}

Comments

0
<h2>React Fetch API Example</h2>
    <ul>
      {Users.map((user) =>
        Object.keys(user).map((key) => (
          <li>
            {user[key].id} - {user[key].advice}
          </li>
        ))
      )}
    </ul>

Comments

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.