我目前正在將我的 ReactJS 代碼移動到 React Native,我想知道我是否需要以任何方式更新它。它正在工作,但我相信某個地方可能存在內存泄漏(我在運行應用程序時收到警告)。我所指的部分是:class Stopwatch extends Component { constructor() { super(); this.state = { timerOn: false, timerStart: 0, timerTime: 0 }; } startTimer = () => { this.setState({ timerOn: true, timerTime: this.state.timerTime, timerStart: Date.now() - this.state.timerTime, }); this.timer = setInterval(() => { this.setState({ timerTime: Date.now() - this.state.timerStart }); }, 10); }; stopTimer = () => { this.setState({ timerOn: false, }); clearInterval(this.timer); }; render() { return ( <View> {this.state.timerOn === false && this.state.timerTime === 0 && ( <TouchableOpacity style={styles.button} onPress={this.startTimer}> <Text>start</Text> </TouchableOpacity> )} {this.state.timerOn === true && ( <TouchableOpacity style={styles.button} onPress={this.stopTimer}> <Text>stop</Text> </TouchableOpacity> )} </View>}<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
本機反應 - 為計時器應用程序優化 ReactJS 代碼
蝴蝶刀刀
2022-09-29 16:57:23