React form Explanation

Source: Internet
Author: User
I. Non-controllable components and controllable components: <input type= "text" defaultvalue= "HelloWorld"/> Data write dead. Controllable components: <input type= "Text" defaultvalue={this.state.value}/> why components are controllable: data stream data that conforms to react is stored in state for ease of use for processing data
Non-controllable component code:
<! DOCTYPEHTML> <HTMLlang="ZH-CN"> <Head> <Metacharset="UTF-8"> <title> Form Details </title> </Head> <Body> <Scriptsrc="./build/react.js"></Script> <Scriptsrc="./build/jsxtransformer.js"></Script> <Scripttype="TEXT/JSX">/*var MyForm = React.createclass ({render:function () {return <input type= "text" DEFAULTV Alue= "helloworld!" />}}); */varMyForm=React.Createclass({submithandler:function(event) {Event.preventdefault ();varHelloto =React.Finddomnode( This.Refs. Helloto).value; alert (Helloto); }, Render:function() {return<formonsubmit={ This.submithandler}> <inputtype="Text"ref="Helloto"defaultvalue="Hello World"/><BR/> <Buttontype="Submit">speak</Button> </form>});React.Render(<MyForm></MyForm,Document.Body ); </Script> </Body> </HTML>

Controllable Component code:
<! DOCTYPEHTML> <HTMLlang="ZH-CN"> <Head> <Metacharset="UTF-8"> <title> Form Details </title> </Head> <Body> <Scriptsrc="./build/react.js"></Script> <Scriptsrc="./build/jsxtransformer.js"></Script> <Scripttype="TEXT/JSX">varMyForm=React.Createclass({getinitialstate:function() {return{Helloto:"HelloWorld" }; }, HandleChange:function() { This. setState ({Helloto:Event.Target.value }); }, Submithandler:function(event)
            {Event.preventdefault (); Alert This. State.Helloto); }, Render:function() {return<formonsubmit={ This.submithandler}> <inputtype="Text"value={ This. State.Helloto}onchange={ This.handlechange}/><BR/> <Buttontype="Submit">speak</Button> </form>});React.Render(<MyForm></MyForm,Document.Body ); </Script> </Body> </HTML>

Ii. use of different form elements <label htmlfor= "name" >Name</label> <input type= "checkbox" value= "A" checked={ this.state.checked} onchange={this.handlechange}/> <textarea Value={this.state.helloto} onchange={ This.handlechange}/> <select Value={this.state.helloto} onchange={this.handlechange}> <option value= " The One ">1</option></select> example code is as follows:
<! DOCTYPEHTML> <HTMLlang="ZH-CN"> <Head> <Metacharset="UTF-8"> <title> Form Details </title> </Head> <Body> <Scriptsrc="./build/react.js"></Script> <Scriptsrc="./build/jsxtransformer.js"></Script> <Scripttype="TEXT/JSX">varMyForm=React.Createclass({getinitialstate:function() {return{username:"",Gender:"Man",checked:true }; }, Handleusernamechange:function() { This. setState ({username:Event.Target.value }); }, Handlegenderchange:function(event) { This. setState ({Gender: Event.Target.value }); }, Handlecheckboxchange:function(event) { This. setState ({checked: Event.Target.checked }); }, Submithandler:function() {Event. Preventdefault ();Console. log ( This. State); }, Render:function() {return<formonsubmit={ This.submithandler}> <labelhtmlfor="username"> Please enter your user name: </label> <inputid="username"type="Text"value={ This. State.username}onchange={ This.handleusernamechange}/><BR/> <Selectvalue={ This. State.Gender}onchange={ This.handlegenderchange}><optionvalue="Man"> Male </option> <optionvalue="Woman"> Female </option></Select> <BR/> <labelhtmlfor="checkbox"> Agree User agreement: </label> <inputid="checkbox"type="checkbox"value="Agree"checked={ This. State.checked}onchange={ This.handlecheckboxchange}/> <ButtonTyp
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.