我剛剛開始學(xué)習(xí)一些本機(jī)反應(yīng),我正在嘗試制作一個(gè)簡(jiǎn)單的 cookie 點(diǎn)擊器應(yīng)用程序。當(dāng)我點(diǎn)擊我的 cookie 時(shí),cookiecount 會(huì)增加 1,但我希望 cookie 計(jì)數(shù)每秒也增加 1。當(dāng)我多次點(diǎn)擊 cookie 并且 cookiecount 翻轉(zhuǎn)并跳轉(zhuǎn)到不同的數(shù)字并再次返回時(shí),我認(rèn)為這與我同時(shí)更新 cookiecount 狀態(tài) 2 次有關(guān)?export default function App(props) { {/* Make the state that holds the amount of cookies */} const [cookieCount, modifyCookieCount] = useState(0) const [costCount, modifyCostCount] = useState(10) const [perClickBoost, setBoost] = useState(1) const interval = setInterval(() => modifyCookieCount(cookieCount+1), 1000);function addPurchase(count){ modifyCookieCount(cookieCount-count) modifyCostCount(costCount+10); setBoost(perClickBoost+1) } return ( <View style={styles.container}> <Text>{cookieCount}</Text> {/* TouchableOpacity is needed to make use of Onpress prop */} <TouchableOpacity onPress = {addCookie}> <Image style={styles.image} source={require('./images/cookie.png')}/> </TouchableOpacity> <Purchasables addPurchase={addPurchase} count={cookieCount} title='Koekjesslaaf' cost={costCount}/> <StatusBar style="auto" /> </View> ); function addCookie(){ {/* Add a cookie */} modifyCookieCount(cookieCount+perClickBoost) } }該問題是由這兩個(gè)更新引起的: const interval = setInterval(() => modifyCookieCount(cookieCount+1), 1000);和 function addCookie(){ {/* Add a cookie */} modifyCookieCount(cookieCount+perClickBoost) }我希望有人可以幫助我解決這個(gè)問題,如果我的代碼沒有意義,我很抱歉,我剛剛開始學(xué)習(xí)。
React Native同時(shí)修改相同的狀態(tài)
慕森卡
2023-11-11 21:49:36