Home > other >  React native , incrementation
React native , incrementation

Time:06-08

I have a variable pass and I did a {pass } inside of one of my return. Howether , while it increment the pass, it also show the pass on my page. How to make it so that it is not on my page? Here is my code:

return (
<View>
  <Text>
    //Here I put some text, then I wanted to increment
    {pass  }
  </Text>
</View>
)

CodePudding user response:

You could achieve using 2 useEffect hooks:

const [pass, setPass] = useState(0);
 
useEffect(() => {
   setPass((prev) => prev   1);
});

useEffect(() => {
   console.log(pass);
},[pass]);

With first one you increment pass by one every time Text will be rendered; with the second one you print last pass value.

  • Related