React.js High-order components----simple examples to understand higher-order component ideas

Source: Internet
Author: User

/** The higher-order component is actually a function that passes in a component that returns a new component * to implement logical multiplexing in different components, * to reuse some logic that can be extracted separately from the new component to be returned * and then pass a separate component to the new component **/import React, {Component} from' React 'Import Reactdom from' React-dom '//High -order component definition, return inside returns new componentfunctionLocal (Comp,key) {class Proxy extends component{//The constructor constructor defines your stateConstructor () {super ();  This. State={data: "}        }        //hook function, before component renderingComponentwillmount () {Let data=Localstorage.getitem (key);  This. SetState ({data})} Handblur= (Event) = ={Let data=Event.target.value;        Localstorage.setitem (Key,data); } render () {//Comp is a component passed in            return(                <comp handblur={ This. Handblur} data={ This.state.data}/>            )        }    }    returnProxy}//Define your individual componentsfunctionInput (props) {return<input type= "text" Defaultvalue={props.data} onblur={props.handblur}/>}functionTextareas (props) {return<textarea defaultvalue={props.data}></textarea>}functionDiv () {return<div>111</div>}//call a higher-order function to return a new functionLet Localinput=local (Input, ' username ') Let Localtextareas=local (textareas, ' content ') Let Localdiv=Local (DIV)//the total component to renderclass from extends Component {render () {return (            <div> <form>User name<LocalInput/> capacity <LocalTextareas/> <LocalDiv></LocalDiv> </form> </div>)}}reactdom.render (<from></from>,document.queryselector ("#root"))

React.js High-order components----simple examples to understand higher-order component ideas

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.