Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Button activating by itself in react native expo

How come whenever I move from a screen to another screen, my button get automatically pressed straight away without me even pressing on it.

My code (Review screen)

import { StyleSheet, Text, View, SafeAreaView, TextInput, TouchableOpacity, Button } from 'react-native'
import React , {useEffect, useState} from 'react'
import {ref, onValue} from 'firebase/database'
import {db} from '../firebase'

const Reviews = ({route}) => {
  const paramKey = route.params.paramKey1
  var ke;
  const [todoDatak, setToDoDatak] = useState([])
  const [todoDatafb, setToDoDatafb] = useState([])
  
  useEffect (() => {
    const starCountRef = ref(db, "food/" + paramKey);
    onValue(starCountRef, (snapshot) =>{
      
      ke = snapshot.key;
      setToDoDatak(ke)
     
      });
    })
  return (
    <View style = {styles.container}>
       
       <Text style = {styles.header}>Shop Name</Text>
       <Text style = {styles.text}>{todoDatak}</Text>
      
       <View style={{
        borderBottomColor: 'black',
        borderBottomWidth: StyleSheet.hairlineWidth,
        }}
      />
      
      <View style = {styles.inputContainer}> 
            <Text></Text>
            <Text>   Feedback: </Text>
            
            <TextInput
                editable
                multiline
                numberOfLines={5}
                maxLength={500}
                placeholder = "Key in for any feedback for the shop" 
                //value={email}
                onChangeText ={text => setToDoDatafb(text)}
                style = {styles.input} 
            />

            <TouchableOpacity
                onPress = { console.log("Pressed")}
                style = {styles.text}
            >
                <Text style={styles.textP}>Post</Text>
            </TouchableOpacity>
        </View> 

       
        
    </View>

    

    
    
  )
}

export default Reviews

Upon loading this page, the button is pressed automatically 3 times

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Shown in my log

enter image description here

But if i try to manually click the button, nothing happen. No new logs of ‘Pressed’ appearing in my logs.

How do I prevent the page from clicking the button by itself? Also how do I fix the issue of me manually clicking the button but it did not work?

>Solution :

Try this () => console.log('Pressed')

<TouchableOpacity
  onPress={() => console.log('Pressed')}
  style={styles.button}> ...

What is happening? You are not pressing anything, React is calling your console.log() on every render. Because of the () at the end. Making it a function call. Similar to how you would call any other function in your code.
Instead we should pass just the head of the function, telling Javascript to "call this function when the user presses", in this case we pass an anonymous arrow function, but we don’t immediately call it.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading