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£©
¡¾Íê¡¿--ϲ»¶ÕâƪÎÄÕµĻ°²»·Á¹Ø×¢Ò»ÏÂÎÒÓ´
¡¡