How to make continuous increment with React Native Touchable?

Asked

Viewed 38 times

0

Is there any property in the TouchableOpacity React Native which, when I pressed the increment button and held the touch, continued incrementing the values?

For example, I have this button:

<TouchableOpacity
   style={styles.button} 
   onPress={()=>increment(item.id,item.amount)}
 >
  <Text> + </Text>
</TouchableOpacity> 

That renders like this:

inserir a descrição da imagem aqui

I would like to continue to increase the value by pressing the button.

2 answers

-1

Hello, Touchableopacity and Touchablewithoutfeedback have the options of onPressIn and onPressOut, I believe this will help solve the problem.

You can have a state controlling if the button is still being pressed and make a loop to go incrementing.

  • 2

    Hello, Iury! Your answer may solve the problem, but I suggest you put the necessary code to reproduce it (read on [mcve]). By placing the code, your answer will have more value to future visitors (and also to the author of the question), as it will facilitate the understanding and reproduction of the solution.

-1

The shape I found was using onPressIn and onPressOut with setTimeout.

const App = () => {

 const [count, setCount] = useState(0);
 const [timer,setTimer] = useState(null);

const handlePressIn = () =>{
 // A cada 200ms enquanto estiver pressionando o botão ele vai 
 // executar a função
 setCount(prevCount => prevCount + 1);
 setTimer(setTimeout(handlePressIn, 200));
}

const handlePressOut = () =>{
 clearTimeout(timer);
}

return ( ... )

Now I will try to replicate for my use case.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.