繁体   English   中英

React - TypeError:无法设置未定义的属性'props'

[英]React - TypeError: Cannot set property 'props' of undefined

我正在尝试学习 class 组件,但我做错了但不知道是什么。

import React from "react"

class App extends React.Component() {
    constructor() {
        super()
        this.state = {
            name: "test",
            age: 31
        }
    }
    
    render() {
        return (
            <div>
                <h1>{this.state.name}</h1>
                <h3>{this.state.age} years old</h3>
            </div>
        )    
    }
}
export default App

这就是错误

TypeError: Class extends value undefined is not a constructor or null

您正在扩展React.Component()它应该是React.Component

class App extends React.Component {
  constructor() {
    super();
    this.state = {
      name: "test",
      age: 31
    };
  }

  render() {
    return (
      <div>
        <h1>{this.state.name}</h1>
        <h3>{this.state.age} years old</h3>
      </div>
    );
  }
}
export default App;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM