【问题标题】:App crashes on adding data in a hook very fast应用程序在挂钩中快速添加数据时崩溃
【发布时间】:2022-01-26 12:23:37
【问题描述】:
const [count,setCounter] = useState(0)


const increaseCounter = () =>{
setCounter(value=>value+1)
}

<Button onPress={()=>increaseCounter()}/>

我在上面运行代码并在应用程序中显示计数器。但它使我的应用程序崩溃。因为我做得非常快。无法理解为什么会发生这种情况。请告诉我如何将它保持几毫秒,以便我以异步方式工作。

【问题讨论】:

  • 你试过count +1吗?

标签: react-native react-hooks hook react-functional-component


【解决方案1】:

试试:

const [count,setCounter] = useState(0)
        
const increaseCounter = () => {
setCounter(count + 1);
}
        
<Button onPress={()=>increaseCounter()}/>

【讨论】:

    【解决方案2】:

    你可以试试react docks example

    import React, { useState } from 'react';
    
    function Example() {
      // Declare a new state variable, which we'll call "count"
      const [count, setCount] = useState(0);
    
      return (
        <div>
          <p>You clicked {count} times</p>
          <button onClick={() => setCount(count + 1)}>
            Click me
          </button>
        </div>
      );
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-21
      相关资源
      最近更新 更多