1. Methods for implementing components:
1. Implement components through JS functions<div id="app"></div> <script type="text/babel"> var ReactDiv = document.getElementById('app'); function GetReactComp(){ return <p>I am a react component</p> } const hellocomp = < GetReactComp /> //Capitalize the first letter const reactSpan = ( <span> { hellocomp } </span> ) ReactDOM.render(reactSpan, ReactDiv) </script> 2. Implement components through ES6 class<div id="class"></div> <script type="text/babel"> var reactDiv1=document.getElementById('class'); //Define class component class MyReactComp extends React.Component{ render(){ return ( <h2>Class Components</h2> ) } } //Use class component const testDiv = ( <div>{<MyReactComp/>}</div> ) //Mount ReactDOM.render(testDiv,reactDiv1) </script> 2. Props component value transfer React has a strict protection mechanism for props. Once a value is given, <div id="app"></div> <script type="text/babel"> var reactDiv = document.getElementById('app'); class ReactClassComp extends React.Component { render(){ return ( <div> <p>Username: <input type="text" value={ this.props.name }/></p> <p>Gender: <input type="text" value={ this.props.gender }/></p> </div> ) } } ReactClassComp.defaultProps={ name:'Liu Bei', gender:'male' } const reactp = ( <span> {<ReactClassComp />} </span> ) ReactDOM.render(reactdiv,reactdiv) </script> Note: In many cases, the content of the component needs to be refreshed according to the refresh of the data. At this time, you need to use the State provided by React. 3. State
1. React life cycle mounting (mount):When a component instance is created and inserted into the DOM (1) Constructors are only used in the following two cases:
Note: Do not call the setState() method in the constructor() function. If you need to use internal state, you can directly assign this.state to initialize state in the constructor. constructor(props){ super(props); this.state = { date:new Date() } this.handleShow = this.handleShow.bind(this) } (2) It checks this.props and this.state for changes and returns one of the following types:
(3)
Note: You can call setState() directly in ComponentDidMount(). It will trigger an additional render, but this render will happen before the browser updates the screen. This ensures that even if render() is called twice, the user will not see the intermediate state. renew: compomentDidUpdate(prevProps){ if(this.props.userID !== prevProps.userID){ this.fetchData(this.props.userID) } } Note: If you call setState() in compositionDidUpdate(), you need to wrap it in a conditional statement, otherwise it will cause an infinite loop. It will also cause an extra re-render, which, while invisible to the user, will affect component performance. uninstall: 2. Lifecycle instance-->Clock:<div id="app"></div> <script type="text/babel"> var reactDiv = document.getElementById('app') //Define the class component MyStateComp class MyStateComp extends React.Component { //Constructor constructor(props) { super(props); // Initialize state internally through this.state this.state = { date:new Date(), show:false, text:'display' } //Bind an instance to the event handling function this.handleShow = this.handleShow.bind(this) } //Add subscription componentDidMount() { this.timerID = setInterval(()=>this.tick(),1000) } //Time function tick() { this.setState({ //setState updates the state of the component date:new Date() }) } //Instance display, hide handleShow() { this.setState(state=>({ show: !state.show, text: !state.show?'Hide':'Show' })) } //Component uninstallation: clear the timer componentWillUnmont() { clearInterval(this.timerID) } render() { let isShow = this.state.show; let element; if(isShow){ element = <h2 >{this.state.date.toLocaleTimeString()}</h2> }else{ element = null } return ( <div> <button onClick={this.handleShow}>{this.state.text}time</button> {element} </div> ) } } const reactSpan = ( <span> {<MyStateComp/> } </span> ) ReactDOM.render(reactSpan, reactDiv) </script> This is the end of this article about React State and life cycle. For more relevant React State life cycle content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: How to use Nexus to add jar packages to private servers
>>: How to install and modify the initial password of mysql5.7.18
In the process of web project development, we oft...
illustrate DML (Data Manipulation Language) refer...
1. Download https://dev.mysql.com/downloads/mysql...
During the project optimization today, MySQL had ...
Table of contents 1. Install the psutil package S...
This article is a self-written imitation of the X...
1. Enter start in the menu bar and click startup ...
Before you begin Have a cloud server, mine is Ten...
Copy code The code is as follows: <html> &l...
Writing method 1: update sas_order_supply_month_p...
Table of contents Preface props context state Sum...
IDEA is the most commonly used development tool f...
Table of contents 1. What is virtual dom? 2. Why ...
Use v-model to bind the paging information object...
cause When executing the docker script, an error ...