0

I have a small component and I am trying to trigger a function onPress. I first tried the following: onPress={this.slider} and I did not receive a console log, then I tried onPress={()=>{this.slider}} and still did not receive a console log. Finally I tried onPress={console.log("Hello")} and received a console log. So I am clearly not calling the Slider function properly. I am not sure what I am doing incorrectly - something silly I am sure. All help is appreciated.

class newWave extends Component {

    state = {
        format: "TTS"
    }

    slider = () => {
        console.log("hi");
        if (this.state.format === "TTS")
            this.setState({ format: "Voice" })
        else if (this.state.format === "Voice")
            this.setState({ format: "TTS" })
    }

    render() {

        return (
            <View style={{height:"100%", width: "100%", flexDirection:"column", justifyContent: "center", alignItems:"center"}}>
            <View style={{height: "75%", width:"75%", backgroundColor:"blue", borderRadius:20, flexDirection:"row", justifyContent:"flex-end"}}>
                <View style ={{backgroundColor:"transparent", height: "10%", width:"30%", margin: "2%"}}>
                <View style = {{backgroundColor:"orange", height: "100%", width:"100%", borderRadius: 20,  flexDirection:"row", justifyContent:this.state.format==="TTS"?"flex-start":"flex-end", alignItems:"center"}}>
                <View style = {{backgroundColor:"green", height: "98%", width:"75%", borderRadius: 20, marginLeft: "1%", marginRight: "1%"}} onPress={()=>{this.slider}}></View>
                </View>
                </View>
            </View>
        </View>

        );
    }
}


export default newWave;
1
  • Just change View component in Button or TouchableOpacity Commented Aug 28, 2019 at 8:41

6 Answers 6

2

Let me introduce you to TouchableOpacity. The View doesn't provide an onPress prop.

<TouchableOpacity onPress={()=> {
    this.slider();
    }
  }>
   <View style = {{backgroundColor:"green", height: "98%", width:"75%", borderRadius: 20, marginLeft: "1%", marginRight: "1%"}} ></View>
</TouchableOpacity>

Sign up to request clarification or add additional context in comments.

1 Comment

About the one time you saw the console.log, onPress={console.log("Hello")}, is because putting the brackets "()" will trigger the function wherever they are, logging it during every render. If you kept the code and tried pressing on the onPress you would notice that it won't actually trigger
2

In React Native, there are three ways to call onPress: TouchableNativeFeedback, TouchableHighlight and TouchableOpacity

so wrap the view inside any of this three, this three has onPress props.

Comments

0

Use TouchableOpacity's onPress() prop.

Also, there are two ways to bind callbacks in React.

  1. Using public class field syntax
const slider = () => {}

<TouchableOpacity onPress={this.slider}>
  ...
</TouchableOpacity>
  1. Using Arrow function
function slider() {}

<TouchableOpacity onPress={() => this.slider()} />
  ...
</TouchableOpacity>

Comments

0

Please try this. This should work. View doesn't come with onPress event

    <Button 
title='Hello'
style = {{backgroundColor:"green", height: "98%", width:"75%", borderRadius: 20, marginLeft: "1%", marginRight: "1%"}} onPress={()=>{this.slider()}}>
</Button>

Comments

0

Try this code:

<TouchableOpacity
  style={{
    backgroundColor: 'green',
    height: '98%',
    width: '75%',
    borderRadius: 20,
    marginLeft: '1%',
    marginRight: '1%',
  }}
  onPress={this.slider}
/>

I would highly recommend using a stylesheet for defining your styles.

Comments

0

you need to wrap the view where you want to click with a <TouchableOpacity /> and then pass the onPress={} to the TouchableOpacity, as follows

<TouchableOpacity onPress={CALL_YOUR_FUNCTION_HERE}>
   <View .... /> //add your view here
</TouchableOpacity>

You can also use <Button /> or <TouchableWithoutFeedback /> from react-native depending on your need and condition. Refer to react-native docs, it is written in a clear way, you can find whatever you need there

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.