【问题标题】:Reactjs clicking on button doesnt work after adding webpack and babel to my project将 webpack 和 babel 添加到我的项目后,Reactjs 单击按钮不起作用
【发布时间】:2021-03-30 07:29:21
【问题描述】:

在添加 webpack 之前这是有效的,在添加之后它就停止了。

export class Main extends Component { 
        constructor(props) {
            super(props);
            this.state = {
                currentPage: states.allProducts
            };
           
            this.handleClick = this.handleClick.bind(this);
            
        }
        async componentDidMount() {
            let currentPage = this.state.currentPage;
        }
       
        handleClick() {
            alert('a');
        }
        render() {
            return (
                <button onClick={this.handleClick}>
                    Say hello
                </button>
            );
        }
    }

【问题讨论】:

  • 状态对象中的消息键在哪里?

标签: reactjs webpack babeljs


【解决方案1】:

强烈推荐使用 create-react-app。 它会自己设置和安装所有的 babel 插件和预设,所以你不会遇到这样的问题。

这里提到How To Use Async Await in React

现在,create-react-app 支持开箱即用的 async/await。但是如果你有一个 webpack 样板,你可能会遇到一个错误(稍后会更多)。

是因为缺少一些 babel 插件,你可以安装它们,希望一切都会再次正常运行

【讨论】:

  • 但是如果我想更改 webpack 怎么办。配置,可以吗?
  • 是的,但你还必须安装其他包如cra-webpack-config,medium article for set up
猜你喜欢
  • 2019-11-26
  • 1970-01-01
  • 1970-01-01
  • 2020-02-17
  • 1970-01-01
  • 1970-01-01
  • 2013-09-27
  • 2018-09-05
  • 2020-09-11
相关资源
最近更新 更多