国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Table of Contents
4.React forms and events" >4.React forms and events
Example 1: We can update state when the input box value changes . We can use the onChange event to monitor input changes and modify the state. " >Example 1: We can update state when the input box value changes . We can use the onChange event to monitor input changes and modify the state.
Home Web Front-end JS Tutorial How to use react? Detailed explanation of the usage of react (with examples included)

How to use react? Detailed explanation of the usage of react (with examples included)

Sep 11, 2018 pm 04:10 PM
react

This article mainly introduces the use of react. Interested students can click in to see about the use of react. Let us take a look at the content of this article

1. React Props

1. Use props

The main difference between state and props is that props are immutable, while state can be modified based on user interaction to change. This is why some container components need to define state to update and modify data. Subcomponents can only pass data through props.

The name attribute in the instance is obtained through this.props.name.

2. Default props

You can set default values ??for props through the getDefaultProps() method.

2. Component API

1. Set state: setState

setState(objectnextState[, function callback])

( 1) Parameter description:

nextState, the new state to be set, which will be merged with the current state

callback, optional parameters, callback function. This function will be called after setState is set successfully and the component is re-rendered.

Merge nextState and current state, and re-render the component. setState is the main method to trigger UI updates in React event handling functions and request callback functions.

(2) About setStaate

The state cannot be modified through this.state inside the component, because the state will be replaced after calling setState().

setState() does not immediately change this.state, but creates a state that is about to be processed. setState() is not necessarily synchronous. In order to improve performance, React will perform state and DOM rendering in batches.

setState() will always trigger a component redraw unless some conditional rendering logic is implemented in shouldComponentUpdate().

2. Replacement state: replaceState

replaceState(object nextState[, functioncallback])

nextState, the new state to be set, This state replaces the current state.

callback, optional parameters, callback function. This function will be called after replaceState is set successfully and the component is re-rendered.

The replaceState() method is similar to setState(), but the method will only retain the state in nextState, and the original state that is not in nextState will be deleted.

3.????????Set properties: setProps

setProps(object nextProps[, functioncallback])

nextProps, to be set New attribute, this state will be merged with the current props

callback, optional parameters, callback function. This function will be called after setProps is set successfully and the component is re-rendered.

Set component properties and re-render the component.

4. Replacement properties: replaceProps

replaceProps(object nextProps[, functioncallback])

nextProps, to be set A new property that replaces the current props.

callback, optional parameters, callback function. This function will be called after replaceProps is set successfully and the component is re-rendered.

replaceProps()The method is similar to setProps, but it will delete the original props

5.??????????? Force update: forceUpdate

forceUpdate([function callback])

The forceUpdate() method will cause the component to call its own render() method to re-render the component. Components also call their own render(). However, when the component is re-rendered, this.props and this.state will still be read. If the state has not changed, React will only update the DOM.

The forceUpdate() method is suitable for redrawing components other than this.props and this.state (for example: after modifying this.state). This method is used to notify React that render needs to be called. ()

6. Get DOM node: findDOMNode

DOMElement findDOMNode()

Return value: DOM element DOMElement

If the component has been mounted in the DOM, this method returns the corresponding local browser DOM element. When render returns null or false, this.findDOMNode() will also return null. This method is useful when reading values ??from the DOM, such as getting the value of a form field and doing some DOM operations.

7. Determine the component mounting status: isMounted

bool isMounted()

Return value: true or false, indicating whether the component has been mounted in the DOM (if you want to know more, go to the React Reference Manual column on the PHP Chinese website to learn)

isMounted( ) method is used to determine whether the component has been mounted in the DOM. This method can be used to ensure that the calls to setState() and forceUpdate() in asynchronous scenarios will not go wrong.

3. React component life cycle

1. The component life cycle can be divided into three states:

Mounting: Real DOM has been inserted

Updating: Being re-rendered

Unmounting: Moved out of the real DOM

2. The life cycle methods are:

1) componentWillMount is called before rendering, both on the client and on the server.

2) componentDidMount: Called after the first rendering, only on the client side. Afterwards, the component has generated the corresponding DOM structure, which can be accessed through this.getDOMNode(). If you want to use it with other JavaScript frameworks, you can call setTimeout, setInterval or send AJAX requests and other operations in this method (to prevent foreign operations from blocking the UI).

3) componentWillReceiveProps is called when the component receives a new prop (after update). This method will not be called when initializing render.

4) shouldComponentUpdate Returns a Boolean value. Called when the component receives new props or state. Not called during initialization or when using forceUpdate.
Can be used when you confirm that you do not need to update components.

5) componentWillUpdate is called when the component receives new props or state but has not yet rendered. Will not be called during initialization.

6) componentDidUpdate Called immediately after the component completes the update. Will not be called during initialization.

7) componentWillUnmount is called immediately when the component is removed from the DOM.

4.React forms and events

Example 1: We can update state when the input box value changes . We can use the onChange event to monitor input changes and modify the state.

5. React Refs

1. Use method

to bind a ref attribute to the return of render Value:

##

In other codes, obtain the support instance through this.refs :

var input = this.refs.myInput;

var inputValue = input.value;

var inputRect = input.getBoundingClientRect();

2. Example

The input box gets focus after the child clicks the button.

v

ar MyComponent = React.createClass({
      handleClick: function() {
        // 使用原生的 DOM API 獲取焦點
        this.refs.myInput.focus();
      },
      render: function() {
        // 當(dāng)組件插入到 DOM 后,ref 屬性添加一個組件的引用于到 this.refs
        return (
          <p>
            <input type="text"ref="myInput" />
            <input
              type="button"
              value="點我輸入框獲取焦點"
              onClick={this.handleClick}
            />
          </p>
        );
      }
    });
 
    ReactDOM.render(
      <MyComponent />,
      document.getElementById(&#39;example&#39;)
    );

This article ends here (if you want to see more, go to the PHP Chinese website

React User Manual column to learn), there are If you have any questions, you can leave a message below.

The above is the detailed content of How to use react? Detailed explanation of the usage of react (with examples included). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

React Router User Guide: How to implement front-end routing control React Router User Guide: How to implement front-end routing control Sep 29, 2023 pm 05:45 PM

ReactRouter User Guide: How to Implement Front-End Routing Control With the popularity of single-page applications, front-end routing has become an important part that cannot be ignored. As the most popular routing library in the React ecosystem, ReactRouter provides rich functions and easy-to-use APIs, making the implementation of front-end routing very simple and flexible. This article will introduce how to use ReactRouter and provide some specific code examples. To install ReactRouter first, we need

Integration of Java framework and front-end React framework Integration of Java framework and front-end React framework Jun 01, 2024 pm 03:16 PM

Integration of Java framework and React framework: Steps: Set up the back-end Java framework. Create project structure. Configure build tools. Create React applications. Write REST API endpoints. Configure the communication mechanism. Practical case (SpringBoot+React): Java code: Define RESTfulAPI controller. React code: Get and display the data returned by the API.

PHP, Vue and React: How to choose the most suitable front-end framework? PHP, Vue and React: How to choose the most suitable front-end framework? Mar 15, 2024 pm 05:48 PM

PHP, Vue and React: How to choose the most suitable front-end framework? With the continuous development of Internet technology, front-end frameworks play a vital role in Web development. PHP, Vue and React are three representative front-end frameworks, each with its own unique characteristics and advantages. When choosing which front-end framework to use, developers need to make an informed decision based on project needs, team skills, and personal preferences. This article will compare the characteristics and uses of the three front-end frameworks PHP, Vue and React.

Vue.js vs. React: Project-Specific Considerations Vue.js vs. React: Project-Specific Considerations Apr 09, 2025 am 12:01 AM

Vue.js is suitable for small and medium-sized projects and fast iterations, while React is suitable for large and complex applications. 1) Vue.js is easy to use and is suitable for situations where the team is insufficient or the project scale is small. 2) React has a richer ecosystem and is suitable for projects with high performance and complex functional needs.

React vs. Vue: Which Framework Does Netflix Use? React vs. Vue: Which Framework Does Netflix Use? Apr 14, 2025 am 12:19 AM

Netflixusesacustomframeworkcalled"Gibbon"builtonReact,notReactorVuedirectly.1)TeamExperience:Choosebasedonfamiliarity.2)ProjectComplexity:Vueforsimplerprojects,Reactforcomplexones.3)CustomizationNeeds:Reactoffersmoreflexibility.4)Ecosystema

React's Role in HTML: Enhancing User Experience React's Role in HTML: Enhancing User Experience Apr 09, 2025 am 12:11 AM

React combines JSX and HTML to improve user experience. 1) JSX embeds HTML to make development more intuitive. 2) The virtual DOM mechanism optimizes performance and reduces DOM operations. 3) Component-based management UI to improve maintainability. 4) State management and event processing enhance interactivity.

React's Ecosystem: Libraries, Tools, and Best Practices React's Ecosystem: Libraries, Tools, and Best Practices Apr 18, 2025 am 12:23 AM

The React ecosystem includes state management libraries (such as Redux), routing libraries (such as ReactRouter), UI component libraries (such as Material-UI), testing tools (such as Jest), and building tools (such as Webpack). These tools work together to help developers develop and maintain applications efficiently, improve code quality and development efficiency.

Netflix's Frontend: Examples and Applications of React (or Vue) Netflix's Frontend: Examples and Applications of React (or Vue) Apr 16, 2025 am 12:08 AM

Netflix uses React as its front-end framework. 1) React's componentized development model and strong ecosystem are the main reasons why Netflix chose it. 2) Through componentization, Netflix splits complex interfaces into manageable chunks such as video players, recommendation lists and user comments. 3) React's virtual DOM and component life cycle optimizes rendering efficiency and user interaction management.

See all articles