Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.1k views
in Technique[技术] by (71.8m points)

reactjs - useEffect to fetch Blizzard API

I am Attempting just to do a fetch on the Blizzard API using useEffect as my hook. The call goes thru successfully however when I attempt to set my key={item.id} and then try to extract the information I want thru {item.race} I get

Failed to compile
./src/App.js
SyntaxError: /Users/gabrielcastro/Development/code/React/convergence/src/App.js: Unexpected token, expected "," (12:2)

  10 |   .then(response => response.races.json())
  11 |   .then(console.log(response.json())
> 12 |   },[races]) 
     |   ^
  13 |  
  14 | 
  15 |   return (

This is what my code looks like... I took out the access token just for this post.

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

export default function App() {

  const [races, setRaces] = useState('races')
  const [items, setItems] = useState([])
  
  useEffect(() => {
  fetch(`https://us.api.blizzard.com/data/wow/playable-race/index?namespace=static-us&locale=en_US&access_token=`)
  .then(response => response.races.json())
  .then(console.log(response.json())
  },[races]) 
 

  return (
    <div>
    {items.map(item => {
   <li key={item.id}>
    Race: {item.name}
  </li>
  })}
     </div> 
     )
    }

below is a snip-it of information I am attempting to access enter image description here

Any help is appreciated!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The .map() method is only available for arrays. The data you're trying to access might be an object. You need to drill down to the races key, which holds the data you need and is in fact an array so you should be able to run .map() on it.

Can you try setting the races attribute in your state to something like response.races to isolate the data you want to work with, then try again.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...