第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

react native 功能組件中構(gòu)造函數(shù)的解決方案是什么?

react native 功能組件中構(gòu)造函數(shù)的解決方案是什么?

慕妹3146593 2022-11-11 16:57:46
讓我?guī)憬鉀Q我的問(wèn)題。我正在制作一個(gè)計(jì)時(shí)器功能組件,我將 startValue 傳遞給組件,然后該組件將使用通過(guò) props 傳遞的 startValue 啟動(dòng)計(jì)時(shí)器(以秒為單位減少一秒)。const FunctionalComponent = (props: any) => {const [timerValue, setTimerValue] = useState(props.initialValue)console.log('Set State')useEffect(() => {    console.log('UseEffects called')    setInterval(() => {        setTimerValue(timerValue - 1)    }, 1000)}, [])return <View><Text style={styles.textStyle}>{timerValue}</Text></View>}我在 Parent 中的渲染功能。render() {    return <View style={styles.mainView}>        <FunctionalComponent  initialValue={30} />    </View>}現(xiàn)在,每次 react 重新渲染父組件時(shí),F(xiàn)unctionalComponent 都會(huì)被調(diào)用并重置 timerValue 值。我使用類組件構(gòu)造函數(shù)解決了這個(gè)問(wèn)題,但我想知道在功能組件中是否有任何解決方案可以做到這一點(diǎn)。class OTPTimer extends Component {    constructor(props: any) {        super(props)        this.state = {            timeLeft: props.fromStart        }        if (props.startTimer) {            this.startTimer()        }    }    componentDidUpdate(prevProps: any) {        if (!prevProps.startTimer && this.props.startTimer) {            this.startTimer()            this.setState({                timeLeft: this.props.fromStart            })        }    }    startTimer = () => {        var interval = setInterval(() => {            this.setState({                timeLeft: this.state.timeLeft - 1            })            if (this.state.timeLeft === 0) {                clearInterval(interval)            }        }, 1000)    }    render() {        return <Text style={globalStyles.testStyleThree}>{`00:${this.state.timeLeft > 9 ? this.state.timeLeft : `0${this.state.timeLeft}`}`}</Text>    }}
查看完整描述

3 回答

?
互換的青春

TA貢獻(xiàn)1797條經(jīng)驗(yàn) 獲得超6個(gè)贊

這就是使用React.memo的意義所在,以防止在子組件的 props 不變時(shí)重新渲染它們。


React.memo 是一個(gè)高階組件。它類似于 React.PureComponent 但用于函數(shù)組件而不是類。


如果你的函數(shù)組件在給定相同的 props 的情況下呈現(xiàn)相同的結(jié)果,你可以將它包裝在對(duì) React.memo 的調(diào)用中,以便在某些情況下通過(guò)記憶結(jié)果來(lái)提高性能。這意味著 React 將跳過(guò)渲染組件,并重用上次渲染的結(jié)果。


    const FunctionalComponent = React.memo<{initialValue: number}>({initialValue}) => {

      const [timerValue, setTimerValue] = useState(initialValue)

    

      console.log('Set State')

    

      useEffect(() => {

          console.log('UseEffects called')

    

          setInterval(() => {

              setTimerValue(timerValue - 1)

          }, 1000)

    

      }, [])

    

      return <View><Text style={styles.textStyle}>{timerValue} 

 </Text></View>


    };


查看完整回答
反對(duì) 回復(fù) 2022-11-11
?
一只名叫tom的貓

TA貢獻(xiàn)1906條經(jīng)驗(yàn) 獲得超3個(gè)贊

出 React.memo,如果子組件的 props 沒有改變,女巫將阻止重新渲染

const FunctionalComponent = React.memo((props: any) => { .... } )



查看完整回答
反對(duì) 回復(fù) 2022-11-11
?
ibeautiful

TA貢獻(xiàn)1993條經(jīng)驗(yàn) 獲得超6個(gè)贊

人們建議useEffect但它會(huì)在渲染后被調(diào)用。


改用useMemo:


useMemo(() => {

  console.log('This is useMemo')

}, []);


查看完整回答
反對(duì) 回復(fù) 2022-11-11
  • 3 回答
  • 0 關(guān)注
  • 120 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)