·²Ê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
¡¡