A stateful component
In addition to accepting input data (through this.props
), the component can also maintain internal state data (through this.state
). When the state data of a component changes, the markup that is displayed is called back to render()
update.
First look at the structure, here is the hook function
hook function:<br>
The first is a system mechanism for replacing interrupts under a DOS system, provided under Windows systems;<br>
Now it is a generic, after the specific system event hook, once the hook event occurs, the event to hook the program, you will receive a system notification. This is the program that responds to the first time the event is made.
//mounting means that React components is generated by render parsing a process that generates the corresponding DOM node and is inserted into the DOM node of the browser and inserted into the browser's DOM structure//order of execution SeevarTimer =React.createclass ({//Initialize stateGetinitialstate:function() { return{secondselapsed:0}; }, tick:function() { //call SetState to re-assign a value of secondselapsed, which will be re-render after the call This. SetState ({secondselapsed: This. state.secondselapsed + 1}); }, //React components life cycle is called after renderComponentdidmount:function() {
Timer Update This. Interval = SetInterval ( This. Tick, 1000); }, //react the components lifecycle is called when the control is destroyedComponentwillunmount:function() {
Clears the current timer clearinterval ( This. Interval); }, Render:function() { return ( //output current secondselapsed value<div>seconds Elapsed: { This.state.secondselapsed}</div> ); }});//Mountnode, bound to the specified ID, generally so write document.getElementById (' app ')React.render (<timer/>, Mountnode);
React website Learning