4

i want to generate a button click dynamically for a TouchableOpacity in react-native, i didn't find anything about that,

all i want is to call the TouchableOpacity onPress from a fuction (or see its effect on the button)

in titanium we were doing $.button.click i tried using Animated but no luck https://facebook.github.io/react-native/docs/animations.html

so can anybody help? thanks in advance

1 Answer 1

11

It's really inadvisable but something like this should work:

simulatePress() {
  this.touchable.props.onPress();
}

render() {
    return (
        <TouchableOpacity ref={component => this.touchable = component} onPress={() => console.log('onPress')}>
          <Text>Tap me</Text>
        </TouchableOpacity
    );
}

Really though, what you are trying to achieve? There is likely a better way to do it.

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

3 Comments

thank you, this worked in calling the method, but not in simulation the effect of the click, i resolved the problem using animatable and i replaced the opacity change with a pulse ...,
what if I have two buttons?
I disagree with this being really inadvisable. If you have a component with children inputs/buttons that has to use it's parent components properties, but you want external controls then this a good solution.

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.