3.13. React constructor() method

发布时间 : 2025-10-25 13:34:29 UTC      

Page Views: 10 views

constructor() : The format of the method is as follows:

constructor(props) 

Before the React component is mounted, its constructor is called constructor() .

React.Component : When a subclass implements a constructor, it should be called before other statements super(props) .

When the following example creates a component, React calls the constructor:

3.13.1. Example

classHeaderextendsReact.Component{constructor(props){super(props);this.state={favoritewebsite:"runoob"};}render(){return(<h1>My favorite websites{this.state.favoritewebsite}h1>);}}ReactDOM.render(<Header/>,document.getElementById('root'));    

In React, constructors are used only in the following two cases:

  • By giving this.state assign an object to initialize the interior state .

  • Bind an instance for the event handler.

If you don’t initialize state or without method binding, you do not need to implement a constructor for the React component.

In constructor() do not call the setState() method. If your component needs to use the internal state directly in the constructor for this.state initial assignment state :

constructor(props) { super(props); // Do not call here this.setState() this.state = { counter: 0 }; this.handleClick = this.handleClick.bind(this); } 

Can only be specified directly in the constructor this.state assign a value. If you need to assign values in other methods, you should use the this.setState() replace.

《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。