Home > database >  App crashes on adding data in a hook very fast
App crashes on adding data in a hook very fast

Time:12-28

const [count,setCounter] = useState(0)


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

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

i am running above code and display counter in the app. But it crashes my app. As i am doing it very fast. Not able to get why is this happening. Please tell me how to hold it for some milliseconds so that i will work in async manner.

CodePudding user response:

You could try 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>
  );
}

CodePudding user response:

Try:

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