HTML5¼¼Êõ

¡¾reactѧϰ¡¿¹ØÓÚreact¿ò¼ÜʹÓõÄһЩϸ½ÚÒªµãµÄ˼¿¼ - ÍâÆŵÄÅíºþÍå(2)

×ÖºÅ+ ×÷ÕߣºH5Ö®¼Ò À´Ô´£ºH5Ö®¼Ò 2017-04-16 18:00 ÎÒÒªÆÀÂÛ( )

class Son extends React.Component{ render(){ return (h3 style ={{marginTop:30}}ÎÒ´ÓÎÒµÄÒ¯Ò¯ÄÇÀïµÃµ½ÁË»ùÒò--{ this .props.gene}/h3) } }class Father extends React.Component{ render(){ return (Son gene

class Son extends React.Component{ render(){ return (<h3 style ={{marginTop:30}}>ÎÒ´ÓÎÒµÄÒ¯Ò¯ÄÇÀïµÃµ½ÁË»ùÒò--{this.props.gene}</h3>) } } class Father extends React.Component{ render(){ return (<Son gene = {this.props.gene}/>) } } class GrandFather extends React.Component{ constructor(props) { super(props) this.state ={ gene:'[Ò¯Ò¯µÄ»ùÒò]' } } render(){ return (<Father gene = {this.state.gene}/>) } }

demo:

 

¡¾(¡£`¦Ø)ËäÈ»ÌýÆðÀ´Óеã¹Ö¹ÖµÄµ«ÊÇ´ó¼Ò±ð½éÒâ¹þ¡¿

 

ʵÏÖÊÇʵÏÖÁË£¬µ«ÄãÏëÏ룬¼ÙÉè²»ÊÇ´Ó¡°Ò¯Ò¯¡±×é¼þ£¬¶øÊÇ´Ó¡°Ì«Ì«Ì«Ì«Ò¯Ò¯¡±×é¼þ´«ÏÂÀ´£¬Õâ¶à¿ÉÅ£¡²»¹ýû¹Øϵ£¬reactÌṩÁËÒ»¸ö½Ð×öcontext(ÉÏÏÂÎÄ)µÄAPI£¬ÄãÔÚ¶¥²ã×é¼þµÄcontextÖж¨ÒåµÄÊôÐÔ£¬¿ÉÒÔÔÚËùÓеĺó´ú×é¼þÖУ¬Í¨¹ýthis.context.ÊôÐÔÈ¥ÒýÓã¡ÈÃÎÒÃÇÒ»¶ÃΪ¿ì£º

class Son extends React.Component{ render(){ console.log(this.context.color); return (<h3 style ={{marginTop:30}}>ÎÒ´ÓÎÒµÄÒ¯Ò¯ÄÇÀïµÃµ½ÁË»ùÒò--{this.context.gene}</h3>) } } Son.contextTypes ={ gene:React.PropTypes.string } class Father extends React.Component{ render(){ return (<Son/>) } } class GrandFather extends React.Component{ getChildContext(){ return {gene:'[Ò¯Ò¯µÄ»ùÒò]'} } render(){ return (<Father />) } } GrandFather.childContextTypes = { gene: React.PropTypes.string }; export default GrandFather

demoЧ¹ûͬÉÏ£¡Õâ¸öʱºòÄã·¢ÏÖ£¬ÎÒÃÇÔÚ<GrandFather>×é¼þºÍ<Father>×é¼þÖж¼Ã»ÓÐÏòÏ´«µÝprops£¬ÎÒÃǾʹÓ×îϲãµÄSon×é¼þÖлñÈ¡ÁËgeneÊôÐÔ£¬ÊDz»ÊǺܷ½±ã£¡

 

½âÊÍÏ´úÂ룺

getChildContext()ÊÇÄãÔÚ¶¥²ã×é¼þÖж¨ÒåµÄ¹³×Óº¯Êý£¬Õâ¸öº¯Êý·µ»ØÒ»¸ö¶ÔÏ󡪡ªÄãÏ£ÍûÔÚºó´ú×é¼þÖÐÈ¡ÓõÄÊôÐԾͷÅÔÚÕâ¸ö¶ÔÏóÖУ¬Æ©ÈçÕâ¸öÀý×ÓÖÐÎÒÏ£ÍûÔÚSon×é¼þÖÐͨ¹ýthis.context.geneÈ¡ÊôÐÔ£¬ËùÒÔÔÚgetChildContext()Öзµ»Ø{gene:'[Ò¯Ò¯µÄ»ùÒò]'}

GrandFather.childContextTypesºÍSon.contextTypes ÓÃÓڹ涨¶¥²ã×é¼þºÍÈ¡¶¥²ã×é¼þcontextµÄºó´ú×é¼þµÄÊôÐÔÀàÐÍ

 

¡¾×¢Òâ¡¿GrandFather.childContextTypesºÍSon.contextTypes ÕâÁ½¸ö¶ÔÏó±ØÐëÒª¹æ¶¨£¡·ñÔòcontextÖ»ÄÜÈ¡µ½¿Õ¶ÔÏó£¡Ò»¿ªÊ¼ÎÒ·¸µÄÕâ¸ö´íÎó¼òÖ±ÈÃÎÒ¿ñÍÂÈýÉýѪ¡£¡£¡£¡£

 

ÓÐͼÓÐÕæÏàÖ®contextºÍpropsµÄÇø±ð

 

3.2contextÊÇ·ñÍƼöʹÓã¿

ËäÈ»ÉÏÃæÕâ¸öÀý×Ó˵Ã÷ÁËcontext¶àôºÃÓ㬵«×¢Ò⣺¹Ù·½²¢²»ÍƼö¾­³£Ê¹ÓÃËü£¬ÒòΪËü»áÈÃÄãµÄÓ¦Óüܹ¹±äµÃ²»Îȶ¨(¹Ù·½ÎĵµÔ­»°If you want your application to be stable, don't use context),ÔÚÎÒ¿´À´£¬ÎªÊ²Ã´ÔÚ´ó¶àÊýÇé¿öÏÂҪʹÓÃprops¶ø²»ÊÇʵÏÖÊý¾ÝÁ÷ÄØ£¬ÒòΪpropsƾ½è×é¼þºÍ×é¼þ¼äÑÏÃܵÄÂß¼­ÁªÏµ£¬Ê¹µÃÄãÄܹ»ÇåÎúµØ¸ú×ÙÓ¦ÓõÄÊý¾ÝÁ÷(it's easy to track the flow of data through your React components with props)µ±È»ÁË£¬Èç¹ûÄãÓöµ½ÉÏÊöµÄÀý×ÓµÄÇé¿ö£¬context»¹ÊÇ´óÓÐñÔÒæµÄ

 

3.3ÐèÒª¸Ä±äcontextÖеÄÊôÐÔʱºò£¬²»ÒªÖ±½Ó¸Ä±äËü£¬¶øÊÇʹÓÃthis.state×÷Ϊý½é£¬Èç¹ûÄãÊÔͼÔÚ¶¥²ã×é¼þµÄstateÖзÅÈëÒ»¸ö¿É±äµÄÊôÐÔÄã¿ÉÒÔÕâÑù×ö£º

getChildContext(){ return {type:this.state.type} }

 

3.4ÔÚÉÏÊöÎÒÏÞÖÆgeneµÄÀàÐÍʱºòÎÒÊÇÕâÑùдµÄ£ºgene: React.PropTypes.string£¬Ê¹ÓÃÁËReactÄÚÖõÄReact.PropTypes°ïÖúÊôÐÔ£¬´ËʱÎҵİ汾Ϊ "react": "15.4.2",ÔÚ15.5µÄ°æ±¾ºóÕâÒ»°ïÖúÊôÐÔ±»·ÏÆú£¬ÍƼöʹÓÃprops-types¿â£¬ÏñÕâÑù£º

const PropTypes = require("Prop-Types"); GrandFather.childContextTypes = { gene: PropTypes.string };

 

µ±È»£¬ÔÚÕâ֮ǰÄãÐèÒªnpm install prop-types

 

4×é¼þÀàÀïÓÐ˽ÓбäÁ¿a£¬Ëüµ½µ×¸Ä·ÅÔÚthis.aÖл¹ÊÇthis.state¶ÔÏóÖУ¨×÷ΪÊôÐÔa£©ÄØ£¿

ÕâµÃ¸ù¾ÝËüÊÇ·ñÐèҪʵʱµÄÖØäÖȾ¾ö¶¨£¬Èç¹û¸Ã±äÁ¿ÐèҪͬ²½µ½±ä»¯µÄUIÖУ¬ÄãÓ¦¸Ã°ÑËü·ÅÔÚthis.state¶ÔÏóÖУ¬Èç¹û²»ÐèÒªµÄ»°£¬Ôò°ÑËü·ÅÔÚthisÖУ¨ÎÞ´úÂëÎÞdemo£©

 

¡¾Íê¡¿--ϲ»¶ÕâƪÎÄÕµĻ°²»·Á¹Ø×¢Ò»ÏÂÎÒÓ´

¡¡

1.±¾Õ¾×ñÑ­ÐÐÒµ¹æ·¶£¬ÈκÎתÔصĸå¼þ¶¼»áÃ÷È·±ê×¢×÷ÕߺÍÀ´Ô´£»2.±¾Õ¾µÄÔ­´´ÎÄÕ£¬ÇëתÔØʱÎñ±Ø×¢Ã÷ÎÄÕÂ×÷ÕߺÍÀ´Ô´£¬²»×ðÖØÔ­´´µÄÐÐΪÎÒÃǽ«×·¾¿ÔðÈΣ»3.×÷ÕßͶ¸å¿ÉÄܻᾭÎÒÃDZ༭Ð޸Ļò²¹³ä¡£

Ïà¹ØÎÄÕÂ
  • ¼ÆËã»úÍøÂ硪¡ªDNSЭÒéµÄѧϰÓëʵÏÖ - ѧÊýѧµÄ³ÌÐòÔ³

    ¼ÆËã»úÍøÂ硪¡ªDNSЭÒéµÄѧϰÓëʵÏÖ - ѧÊýѧµÄ³ÌÐòÔ³

    2017-04-16 10:00

  • ¡¾react¿ò¼Ü¡¿ÀûÓÃshouldComponentUpdate¹³×Óº¯ÊýÓÅ»¯reactÐÔÄÜÒÔ¼°ÒýÈëimmutable¿âµÄ

    ¡¾react¿ò¼Ü¡¿ÀûÓÃshouldComponentUpdate¹³×Óº¯ÊýÓÅ»¯reactÐÔÄÜÒÔ¼°

    2017-04-16 09:02

  • ASP.NET Core MVC Ô´Âëѧϰ£ºÏê½â Action µÄ¼¤»î - Savorboard

    ASP.NET Core MVC Ô´Âëѧϰ£ºÏê½â Action µÄ¼¤»î - Savorboard

    2017-04-14 13:04

  • Ç°¶Ë¿ª·¢¿ò¼Ü¼ò½é:angularºÍreact - ÌÚѶÔƼ¼ÊõÉçÇø

    Ç°¶Ë¿ª·¢¿ò¼Ü¼ò½é:angularºÍreact - ÌÚѶÔƼ¼ÊõÉçÇø

    2017-04-11 18:02

ÍøÓѵãÆÀ
¾