Showing posts with label React installed. Show all posts
Showing posts with label React installed. Show all posts
React AJAX

React AJAX

React component data can be obtained through the componentDidMount method Ajax, when the database from the server can store the data in the state, and then use this.setState method to re-render the UI.
When using asynchronous load data, use componentWillUnmount to cancel the outstanding request before the component is uninstalled.
The following example demonstrates getting the Github user's latest gist share description:

React instance

Var UserGist = React . CreateClass ( { GetInitialState : function ( ) { Return { Username : ' ' , lastGistUrl : ' ' } ; } , ComponentDidMount : function ( ) { This . ServerRequest = $. Get ( this . Props . Source , function ( Result ) { Var LastGist = result [ 0 ] ; this . SetState ( { Username : lastGist . Owner . Login , lastGistUrl : lastGist . Html_url } ) ; } , ComponentWillUnmount : function ( ) { This . ServerRequest . Abort ( ) ; } , render : function ( ) { Return ( < Div > { this . State . Username } user's latest Gist shared address: < a Href = { this . State . LastGistUrl } > { this . State . LastGistUrl } </ a > </ div > ) ; } } ) ; ReactDOM . Render ( < UserGist Source = " https://api.github.com/users/ octocat / gists " />, mountNode ) ;

try it"
The above code uses jQuery to complete the Ajax request.
React component lifecycle

React component lifecycle

In this section we will discuss the lifecycle of the React component.
The component's life cycle can be divided into three states:
  • Mounting: The real DOM has been inserted
  • Updating: being re-rendered
  • Unmounting: Moved out of real DOM
The life cycle methods are:
  • ComponentWillMount is called before rendering , and the client is also on the server side.
  • ComponentDidMount : Called after the first rendering , only on the client. After the component has generated a corresponding DOM structure, which can be accessed by this.getDOMNode (). If you want to use with other JavaScript frameworks, you can call setTimeout, setInterval, or send AJAX requests in this method (to prevent heterogeneous operations from blocking the UI).
  • ComponentWillReceiveProps is called when the component receives a new prop. This method will not be called when the render is initialized.
  • ShouldComponentUpdate returns a boolean value. Called when the component receives a new props or state. It is not called when initializing or when using forceUpdate.
    You can use it when you confirm that you do not need to update the component.
  • ComponentWillUpdate is called when the component receives a new props or state but has not yet rendered it. It will not be called at initialization time.
  • ComponentDidUpdate is called as soon as the component completes the update. It will not be called at initialization time.
  • ComponentWillUnmount is called immediately when the component is removed from the DOM.
A detailed description of these methods can be found in the official documentation .
The following example sets the timer by the componentDidMount method after the Hello component is loaded, resets the transparency of the component every 100 milliseconds and re-renders it:

React instance

Var Hello = React . CreateClass ( { GetInitialState : function ( ) { Return { Opacity : 1.0 } ; } , ComponentDidMount : function ( ) { This . Timer = setInterval ( function ( ) { Var Opacity = this . State . Opacity ; opacity - = .05 ; if ( Opacity < 0.1 ) { Opacity = 1.0 ; } This setState ( { Opacity : opacity } ) ; } . Bind ( this ) , 100 ) ; } , render : function ( ) { Return ( < Div Style = { { opacity : this . State . Opacity } } > Hello { This . Props . Name } </ div > ) ; } } ) ; ReactDOM . Render ( < Hello Name = " world " />, document . Body ) ;

try it"
The following example initializes state , setNewnumber is used to update state . All lifecycle is in the Content component .

React instance

Var Button = React . CreateClass ( { GetInitialState : function ( ) { Return { Data : 0 } ; } , SetNewNumber : function ( ) { This . SetState ( { data : this . State . Data + 1 } ) } , Render : function ( ) { Return ( < Div > < button OnClick = { this . SetNewNumber } > INCREMENT </ button > < Content MyNumber = { this . State . Data } > </ content > </ div > ) ; } } ) Var Content = React . CreateClass ( { ComponentWillMount : function ( ) { Console . Log ( ' Component WILL MOUNT! ' ) } , ComponentDidMount : function ( ) { Console . Log ( ' Component DID MOUNT! ' ) } , ComponentWillReceiveProps : function ( newProps ) { Console . Log ( ' Component WILL RECEIVE PROPS! ' ) } , ShouldComponentUpdate : function ( newProps , newState ) { Return True ; } , componentWillUpdate : function ( nextProps , nextState ) { Console . Log ( ' Component WILL UPDATE! ' ) ; } , ComponentDidUpdate : function ( prevProps , prevState ) { Console . Log ( ' Component DID UPDATE! ' ) } , ComponentWillUnmount : function ( ) { Console . Log ( ' Component WILL UNMOUNT! ' ) } , Render : function ( ) { Return ( < Div > < h3 > { this . Props . MyNumber } </ h3 > </ div > ) ; } } ) ; ReactDOM . Render ( < div > < Button /> </ div >, document . GetElementById ( ' example ' ) ) ;

try it"
React Component API

React Component API

In this section we will discuss the React component API. We will explain the following seven methods:
  • Set the state: setState
  • Replace state: replaceState
  • Set the properties: setProps
  • Replace the property: replaceProps
  • Force update: forceUpdate
  • Get the DOM node: findDOMNode
  • Determine the component mount status: isMounted

Set the state: setState

the setState ( Object NextState [, function the callback ]) 

Parameter Description

  • NextState , the new state to be set, which will be merged with the current state
  • Callback , optional parameter, callback function. The function will be set successfully after setState , and the component is re-rendered.
Merge nextState and the current state and re-render the component. SetState is the main method of triggering UI updates in the React event handler and in the request callback function.

About setState

You can not modify the state inside the component with this.state because the state is replaced after calling setState ().
SetState () does not immediately change this.state, but creates a state to be processed. SetState () is not necessarily synchronized, in order to improve the performance React will be batch execution DOM and DOM rendering.
SetState () always triggers component redraw once, unless some conditional rendering logic is implemented in shouldComponentUpdate ().

Examples

React instance

handleClick } > my point! Click the number of times: { this . State . ClickCount } </ h2 > ) ; } } ) ; ReactDOM . Render ( < Counter />, document . GetElementById ( ' message ' ) ) ; handleClick } > my point! Click the number of times: { this . State . ClickCount } </ h2 > ) ; } } ) ; ReactDOM . Render ( < Counter />, document . GetElementById ( ' message ' ) ) ;

try it"
In the example, the click counter is incremented by clicking the h2 tag.

Replace state: replaceState

ReplaceState ( object nextState [, function callback ]) 
  • NextState , the new state to be set, which replaces the current state .
  • Callback , optional parameter, callback function. This function will be called after replaceState is set and the component is re-rendered.
replaceState () method and setState () is similar, but the method only kept nextState state, the original state is not nextState in the state will be deleted.

Set the properties: setProps

SetProps ( object nextProps [, function callback ]) 
  • NextProps , the new property to be set, which is merged with the current props
  • Callback , optional parameter, callback function. The function is called after the setProps setting is successful and the component is rendered again.
Set the component properties, and re-render the components.
Props is equivalent to the component 's data stream, which is always passed down from the parent component to all subcomponents. When integrating with an external JavaScript application, we may need to pass data to the component or notify the React.render () component that needs to be re-rendered, using setProps () .
Update the component , I can call React.render () again on the node , or change the component properties with the setProps () method to trigger the component to render again.

Replace the property: replaceProps

ReplaceProps ( object nextProps [, function callback ]) 
  • NextProps , will set the new property, which will replace the current props .
  • Callback , optional parameter, callback function. The function will be called after the replaceProps is set and the component is re-rendered.
The replaceProps () method is similar to setProps , but it will remove the original
Props

Force update: forceUpdate

ForceUpdate ([ function callback ])

Parameter Description

  • Callback , optional parameter, callback function. The function is called after the component render () method call.
The forceUpdate () method causes the component to call its own render () method to render the component again, and the component's subcomponent also calls its own render (). However, when the component is rendered again, it will still read this.props and this.state, and if the state does not change, React will only update the DOM.
The forceUpdate () method applies to components such as this.props and this.state redraw (eg, modify this.state), by which the React needs to call render ()
In general, you should try to avoid using forceUpdate (), but only from this.props and this.state read the state and triggered by React render () call.

Get the DOM node: findDOMNode

DOMElement findDOMNode ()
  • Return Value: DOM element DOMElement
If the component is already mounted in the DOM, the method returns the corresponding local browser DOM element. When the render return null or false when, this.findDOMNode () will return null . When reading values ​​from the DOM, the method is useful, such as getting the value of the form field and doing some DOM operations.

Determine the component mount status: isMounted

Bool isMounted ()
  • Returns: true or false indicating whether the component is mounted in the DOM
The isMounted () method is used to determine whether the component is mounted in the DOM. You can use this method to ensure that calls to setState () and forceUpdate () in an asynchronous scene do not go wrong.

Popular Posts