HTML5¼¼Êõ

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

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

·²ÊDzÎÔĹýreact¹Ù·½Ó¢ÎÄÎĵµµÄͯЬ´óÌåÉ϶¼ÄÜÖªµÀ¶ÔÓÚÒ»¸ö×é¼þÀ´Ëµ£¬ÆästateµÄ¸Ä±ä£¨µ÷ÓÃthis.setState()·½·¨£©ÒÔ¼°´Ó¸¸×é¼þ½ÓÊܵÄprops·¢Éú±ä»¯Ê±£¬»áµ¼ÖÂ×é¼þÖØäÖȾ£¬ÕýËùν"ѧ¶ø²»Ë¼ÔòØè"£¬ÔÚ²»¶ÏµÄѧϰÖУ¬ÎÒ¿ªÊ¼Ë¼¿¼ÕâһЩÎÊÌ⣺ 1.setState()º¯ÊýÔÚÈÎ

·²ÊDzÎÔĹýreact¹Ù·½Ó¢ÎÄÎĵµµÄͯЬ´óÌåÉ϶¼ÄÜÖªµÀ¶ÔÓÚÒ»¸ö×é¼þÀ´Ëµ£¬ÆästateµÄ¸Ä±ä£¨µ÷ÓÃthis.setState()·½·¨£©ÒÔ¼°´Ó¸¸×é¼þ½ÓÊܵÄprops·¢Éú±ä»¯Ê±£¬»áµ¼ÖÂ×é¼þÖØäÖȾ£¬ÕýËùν"ѧ¶ø²»Ë¼ÔòØè"£¬ÔÚ²»¶ÏµÄѧϰÖУ¬ÎÒ¿ªÊ¼Ë¼¿¼ÕâһЩÎÊÌ⣺

 

1.setState()º¯ÊýÔÚÈκÎÇé¿ö϶¼»áµ¼ÖÂ×é¼þÖØäÖȾÂð£¿Èç¹ûsetState()ÖвÎÊý»¹ÊÇÔ­À´Ã»Óз¢ÉúÈκα仯µÄstateÄØ£¿

2.Èç¹û×é¼þµÄstateûÓб仯£¬²¢ÇÒ´Ó¸¸×é¼þ½ÓÊܵÄpropsҲûÓб仯£¬ÄÇËü¾ÍÒ»¶¨²»»áÖØäÖȾÂð£¿

3.Èç¹û1£¬2Á½ÖÖÇé¿ö϶¼»áµ¼ÖÂÖØäÖȾ£¬ÎÒÃǸÃÈçºÎ±ÜÃâÕâÖÖÈßÓàµÄ²Ù×÷£¬´Ó¶øÓÅ»¯ÐÔÄÜ£¿

 

ÏÂÃæÎÒ¾ÍÓÃʵÀýһһ̽ÌÖÕâЩÎÊÌ⣺

ûÓе¼ÖÂstateµÄÖµ·¢Éú±ä»¯µÄsetStateÊÇ·ñ»áµ¼ÖÂÖØäÖȾ ¡ª¡ª¡¾»á£¡¡¿

 

import React from 'react' class Test extends React.Component{ constructor(props) { super(props); this.state = { Number:1//ÉèstateÖÐNumberֵΪ1 } } //ÕâÀïµ÷ÓÃÁËsetStateµ«ÊDz¢Ã»ÓиıäsetStateÖеÄÖµ handleClick = () => { const preNumber = this.state.Number this.setState({ Number:this.state.Number }) } render(){ //µ±renderº¯Êý±»µ÷ÓÃʱ£¬´òÓ¡µ±Ç°µÄNumber console.log(this.state.Number) return(<h1 onClick = {this.handleClick} style ={{margin:30}}> {this.state.Number} </h1>) } } export default Test //Ê¡ÂÔreactDOMµÄäÖȾ´úÂë...

demo£º

µã»÷1Ò»¹²15´Î£¬Æä¼ädemoûÓз¢ÉúÈκα仯

 

¿ØÖÆ̨Êä³ö£º£¨ÎÒµã»÷ÁË1Ò»¹²15´Î  _(:3 ¡¹¡Ï)_£©

 

ÄÇôÎÊÌâ¾ÍÀ´ÁË£¬ÎÒµÄUIÃ÷Ã÷¾ÍûÓÐÈκα仯°¡£¬ÎªÊ²Ã´Òª×ö×ÅÖжàÓàµÄÖØäÖȾµÄ¹¤×÷ÄØ£¿°ÑÕ⹤×÷¸øÈ¥µô°É£¡

 

ÓÚÊÇÕâÀïreactÉúÃüÖÜÆÚÖеÄshouldComponentUpdateº¯Êý¾ÍÅÉÉÏÓó¡ÁË£¡shouldComponentUpdateº¯ÊýÊÇÖØäÖȾʱrender()º¯Êýµ÷ÓÃÇ°±»µ÷Óõĺ¯Êý£¬Ëü½ÓÊÜÁ½¸ö²ÎÊý£ºnextPropsºÍnextState£¬·Ö±ð±íʾÏÂÒ»¸öpropsºÍÏÂÒ»¸östateµÄÖµ¡£²¢ÇÒ£¬µ±º¯Êý·µ»Øfalseʱºò£¬×èÖ¹½ÓÏÂÀ´µÄrender()º¯ÊýµÄµ÷Óã¬×èÖ¹×é¼þÖØäÖȾ£¬¶ø·µ»Øtrueʱ£¬×é¼þÕÕ³£ÖØäÖȾ¡£

 

ÎÒÃǶÔÉÏÃæµÄÇé¿ö×öÒ»¸öССµÄ¸Ä¶¯£º

import React from 'react' class Test extends React.Component{ constructor(props) { super(props); this.state = { Number:1 } } //ÕâÀïµ÷ÓÃÁËsetStateµ«ÊDz¢Ã»ÓиıäsetStateÖеÄÖµ handleClick = () => { const preNumber = this.state.Number this.setState({ Number:this.state.Number }) } //ÔÚrenderº¯Êýµ÷ÓÃÇ°ÅжϣºÈç¹ûÇ°ºóstateÖÐNumber²»±ä£¬Í¨¹ýreturn false×èÖ¹renderµ÷Óà shouldComponentUpdate(nextProps,nextState){ if(nextState.Number == this.state.Number){ return false } } render(){ //µ±renderº¯Êý±»µ÷ÓÃʱ£¬´òÓ¡µ±Ç°µÄNumber console.log(this.state.Number) return(<h1 onClick = {this.handleClick} style ={{margin:30}}> {this.state.Number} </h1>) } }

 

µã»÷±êÌâ1£¬UIÈÔȻûÓÐÈκα仯£¬µ«´Ëʱ¿ØÖÆ̨ÒѾ­Ã»ÓÐÈκÎÊä³öÁË£¬Ã»ÓÐÒâÒåµÄÖØäÖȾ±»ÎÒÃÇ×èÖ¹ÁË£¡

 

×é¼þµÄstateûÓб仯£¬²¢ÇÒ´Ó¸¸×é¼þ½ÓÊܵÄpropsҲûÓб仯£¬ÄÇËü¾Í»¹¿ÉÄÜÖØäÖȾÂ𣿡ª¡ª¡¾¿ÉÄÜ£¡¡¿

import React from 'react' class Son extends React.Component{ render(){ const {index,number,handleClick} = this.props //ÔÚÿ´ÎäÖȾ×Ó×é¼þʱ£¬´òÓ¡¸Ã×Ó×é¼þµÄÊý×ÖÄÚÈÝ console.log(number); return <h1 onClick ={() => handleClick(index)}>{number}</h1> } } class Father extends React.Component{ constructor(props) { super(props); this.state = { numberArray:[0,1,2] } } //µã»÷ºóʹnumberArrayÖÐÊý×éϱêΪindexµÄÊý×ÖÖµ¼ÓÒ»£¬ÖØäÖȾ¶ÔÓ¦µÄSon×é¼þ handleClick = (index) => { let preNumberArray = this.state.numberArray preNumberArray[index] += 1; this.setState({ numberArray:preNumberArray }) } render(){ return(<div style ={{margin:30}}>{ this.state.numberArray.map( (number,key) => { return <Son key = {key} index = {key} number ={number} handleClick ={this.handleClick}/> } ) } </div>) } } export default Father

 

¡¡

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

Ïà¹ØÎÄÕÂ
  • net.sz.framework ¿ò¼Ü µÇ¼·þÎñÆ÷¼Ü¹¹ µ¥·þ2 Íò TPS£¨QPS£© - ʧ×ã³ÌÐòÔ±

    net.sz.framework ¿ò¼Ü µÇ¼·þÎñÆ÷¼Ü¹¹ µ¥·þ2 Íò TPS£¨QPS£© - ʧ×ã

    2017-04-13 11:05

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

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

    2017-04-11 18:02

  • WebApp¿ò¼Ü - stumpx

    WebApp¿ò¼Ü - stumpx

    2017-04-06 15:01

  • net.sz.framework ¿ò¼Ü ÇáËɴ·þÎñ---ÈÃÄã¸üרעÂß¼­¹¦ÄÜ---³õ̽ - ʧ×ã³ÌÐòÔ±

    net.sz.framework ¿ò¼Ü ÇáËɴ·þÎñ---ÈÃÄã¸üרעÂß¼­¹¦ÄÜ---³õ̽ -

    2017-04-02 10:11

ÍøÓѵãÆÀ
³