creat-react-app 创建的项目 async/await 报错

import React, { Component } from "react";

class App extends Component {
    constructor() {
        super();
        this.state = { data: [] };
    }

    async componentDidMount() {
        const response = await fetch(`https://api.coinmarketcap.com/v1/ticker/?limit=10`);
        const json = await response.json();
        this.setState({ data: json });
    }

    render() {
        return (
            <div>
                <ul>
                    {this.state.data.map(el => (
                        <li>
                            {el.name}: {el.price_usd}
                        </li>
                    ))}
                </ul>
            </div>
        );
    }
}

export default App;


clipboard.png

clipboard.png

去掉async已经await 就ok了,请问为什么?是需要单独配置吗?

阅读 3k
2 个回答

async/await 是ES2017的语法

引入这个库试试 babel-polyfill
或者使用babel-preset-env

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题