小编rnd*_* om的帖子

谁能解释为什么我收到地图函数错误

我正在尝试了解 api 获取,并且我遵循了这里的其他一些答案,但是我在这个脚本上不断收到一个奇怪的错误:

const fetcher = async () => {
    const url = 'https://banana1.free.beeceptor.com/cars'
    const response = await fetch(url) //once we fetch we get a resposne
    const json_data = await response.json()
    return json_data
}

const Publish = () => {
    const [loading, setLoading] = useState(true)
    const [choices, setChoices] = useState([])

    useEffect(()=>{
        const choiceGetter = async () => {
            await fetcher().then(json_data=>{
                setChoices(json_data);
                setLoading(false);
                console.log(json_data)
            })
        };
        choiceGetter();
    },[])

    return(
        <>
        <h1>
            {loading&&<>loading...</>}
            {!loading&&choices&&
                choices.map( (choice) => {
                    const {option,make} = choice; …
Run Code Online (Sandbox Code Playgroud)

javascript fetch reactjs next.js

3
推荐指数
1
解决办法
559
查看次数