1

I am trying to render the team field of the array that I am getting from the API. console.log(data) successfully logs it, but when I try to render it, it simply does not show anything. Thank you for the help!

Home.js

const Home = () => {

    const [data, setData] = useState();
    const getData = async () => {
        try {
            const res = await fetch('url');
            const data = await res.json();
            setData(data);
            console.log(data);
        } catch (error) {
            console.log(error)
        }
    }

    useEffect(() => {
        getData();
    }, [])

    return (
        <div>
            Home
            {data.map((item, i) => {
                    <h1 key={i}>{item.team}</h1>
            })}
        </div>
    );
}

Data structure of array 0: {Team: 'Cannons', Win: '4', Loss: '2'} 1: {Team: 'Racers', Win: '6', Loss: '0'}

6
  • 1
    You need to return the <h1> component or wrap it in parentheses if you don't want to use the return keyword. Commented Jul 18, 2022 at 3:15
  • @Min {data.map((item, i) => { return <h1 key={i}>{item.team}</h1> })} like this? Commented Jul 18, 2022 at 3:18
  • 1
    Yes. Otherwise nothing will be rendered Commented Jul 18, 2022 at 3:23
  • @Min I've tried with both return and wrapping it in parenthesis and neither of them are working :( Commented Jul 18, 2022 at 3:25
  • Would you be able to post the data structure logged in console Commented Jul 18, 2022 at 3:43

2 Answers 2

4

Your data.map is going to throw an error because when the component renders, it's trying to map a null/undefined value. Set the initial state of data to an empty array or do a check that data is not null before mapping over it. You also need to return the elements from the map

const Home = () => {
    // set initial state to an empty array
    const [data, setData] = useState([]);
    const getData = async () => {
        try {
            const res = await fetch('url');
            const data = await res.json();
            setData(data);
            console.log(data);
        } catch (error) {
            console.log(error)
        }
    }

    useEffect(() => {
        getData();
    }, [])

    return (
        <div>
            Home
            {data.map((item, i) => {
              // Added return
                  return  <h1 key={i}>{item.team}</h1>
            })}
        </div>
    );
}
Sign up to request clarification or add additional context in comments.

7 Comments

An example can be seen here with trying to map a null or undefined array : jsfiddle.net/RyanZee/utezxkcj
Just tried this, still not working :(
I see your issue. <h1 key={i}>{item.team}</h1>. If you look at your data structure you posted, team is spelled with a capital T. So it should be <h1 key={i}>{item.Team}</h1> . Here is a code sandbox : codesandbox.io/s/tender-https-iyogzh?file=/src/App.js
@AeLeung So first it will show a blank page and throw an error Cannot read properties of undefined (reading 'map') because it's trying to map undefined. It will not update the state after that because the app has crashed. With the state set to an empty array. Nothing would be mapped or returned since there is no key team. Only Team. As you can see in the codesandbox, it works. So the error must be somewhere else in the code
@AeLeung reactjs.org/docs/…. Everything gets unmounted
|
0

Assuming your response is an array, try doing

 const [data, setData] = useState([]);

If thats not the case point to correct array attribute. Like

data.array.map....

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.