3.18. React getSnapshotBeforeUpdate () method

发布时间 :2024-03-14 23:00:04 UTC      

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

getSnapshotBeforeUpdate(prevProps, prevState)

getSnapshotBeforeUpdate() method is called before the last rendered output (submitted to the DOM node).

In getSnapshotBeforeUpdate() method, we can access the props and state .

getSnapshotBeforeUpdate() the method needs to be the same as the componentDidUpdate() method, otherwise an error will occur.

The following examples use the getSnapshotBeforeUpdate() method to viewthe update before state object, the instance will display different values before and after the update after a 1-second delay:

3.18.1. Example

classHeaderextendsReact.Component{constructor(props){super(props);this.state={favoritesite:"runoob"};}componentDidMount(){setTimeout(()=>{this.setState({favoritesite:"google"})},1000)}getSnapshotBeforeUpdate(prevProps,prevState){document.getElementById("div1").innerHTML="What was your favorite website before the update:"+prevState.favoritesite;}componentDidUpdate(){document.getElementById("div2").innerHTML="What is your favorite website after the update:"+this.state.favoritesite;}render(){return(<div>
<h1>My favorite website is{this.state.favoritesite}</h1> <divid="div1"></div>
<divid="div2"></div>
</div>);}}ReactDOM.render(<Header/>,document.getElementById('root'));

Principles, Technologies, and Methods of Geographic Information Systems  102

In recent years, Geographic Information Systems (GIS) have undergone rapid development in both theoretical and practical dimensions. GIS has been widely applied for modeling and decision-making support across various fields such as urban management, regional planning, and environmental remediation, establishing geographic information as a vital component of the information era. The introduction of the “Digital Earth” concept has further accelerated the advancement of GIS, which serves as its technical foundation. Concurrently, scholars have been dedicated to theoretical research in areas like spatial cognition, spatial data uncertainty, and the formalization of spatial relationships. This reflects the dual nature of GIS as both an applied technology and an academic discipline, with the two aspects forming a mutually reinforcing cycle of progress.