React Study notes 2

Source: Internet
Author: User

Properties and states of the react
react data is delivered using two things:

1. Attribute (props): passed in from outside the component, within the component by this.props obtaining (innate, not easily changed)

2. State: Set or change within the component, internal to the component (the this.state state is acquired and often changed)

Attribute-specificity, state-passionate
React Property (Props)
  • Attributes are assigned by (parent component) when a component is declared and cannot be modified

  • The way properties are passed is typically passed in when a component is called

  • Properties can make strings, arrays, objects ...

Pass object directly to the component, using {... The form of Object} (preceded by ...)

Example:

1 var _prop = {"title": "Home Page", "url": "Http://gist.github.com"}2 Reactdom.render (< Ele{..._prop}/>,document.getelementbyid ("example"));

React status (state)

Unlike props,state in the interior of the component to declare

Purpose: Mainly to detect whether the current component internal data changes, once the component data has changed, the state has changed

problem

How does 1.state go about declaring and defining?

1 getinitialstate:function() {2return({3 value: ") Default ",4 bool:false5})6 },

2. How to monitor the data changes inside the component? is actually the process of assigning the state a constant value.

Full code:

1 varCompo =React.createclass ({2Getinitialstate:function(){3 return({4Value: "Hello"5 })6 },7HandleChange:function(e) {8 varText =E.target.value;9  This. SetState ({value:text});Ten }, OneRenderfunction(){ A varValue = This. State.value; - return( -<div> the<input type= "Text" onchange={ This. HandleChange} value={value}/><br/> -<span>value:{value}</span> -</div> - ) + } - }) +Reactdom.render (<compo/>,document.getelementbyid ("example"));

Enter the initial state of the page

When the input box is entered, the value of the input box is displayed synchronously below

Mixed use of props and State in actual development and development
data docking, take the show net commodity list for example:

1. Divided into two components, the outer layer is the parent component, the container of this product list

2. Each loop subkey (render template) in the list of items is a sub-component

You can store the changed data in the parent component and put it in the state

Pass the state data as props to the subcomponents, and let the subcomponents parse themselves

Data interface

React Study notes 2

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.