Skip to content

Instantly share code, notes, and snippets.

@jgable
Created March 30, 2015 01:13
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save jgable/43391acf85ccdbf249bc to your computer and use it in GitHub Desktop.
Save jgable/43391acf85ccdbf249bc to your computer and use it in GitHub Desktop.
TouchableHighlight Examples
/**
* Touchable Highlight example (that fires invariant as expected)
*/
'use strict';
var React = require('react-native');
var {
AppRegistry,
StyleSheet,
Text,
TouchableHighlight,
View,
} = React;
var ExampleComponent = React.createClass({
displayName: 'ExampleComponent',
render: function () {
return (
<View style={styles.container}>
<Text>Some Text 1</Text>
</View>
);
}
});
var AwesomeProject = React.createClass({
getInitialState: function () {
return { toggled: false };
},
render: function() {
return (
<View style={styles.container}>
<Text style={styles.welcome}>
Welcome to React Native!
</Text>
<Text style={styles.instructions}>
To get started, edit index.ios.js{'\n'}
Press Cmd+R to reload
</Text>
<TouchableHighlight onPress={() => this.setState({ toggled: !this.state.toggled })}>
<Text>Toggle</Text>
</TouchableHighlight>
{this.renderOther()}
</View>
);
},
renderOther: function () {
if (!this.state.toggled) {
return null;
}
return (
<TouchableHighlight onPress={this.handlePress} underlayColor="white">
<ExampleComponent />
</TouchableHighlight>
);
}
});
var styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
},
});
AppRegistry.registerComponent('AwesomeProject', () => AwesomeProject);
@carl1990
Copy link

I want to know how to implement handlePress method

@Creemli
Copy link

Creemli commented Oct 28, 2015

eh... could u please tell how to hide the alert in the callback function?

@mrboli
Copy link

mrboli commented Jun 1, 2016

@carl1990 me too

@thEpisode
Copy link

@carl1990 me too

@ThiagoNP
Copy link

@carl1990, @mrboli, @thEpisode it would be something like this:

React.createClass({
  getInitialState: function () {
    return { toggled: false };
  },

  handlePress: function() {
    this.setState({ toggled: !this.state.toggled })
  },
  
  render: function() {
    return (
      <View style={styles.container}>
        <Text style={styles.welcome}>
          Welcome to React Native!
        </Text>
        <Text style={styles.instructions}>
          To get started, edit index.ios.js{'\n'}
          Press Cmd+R to reload
        </Text>
        <TouchableHighlight onPress={this.handlePress}>
          <Text>Toggle</Text>
        </TouchableHighlight>
        {this.renderOther()}
      </View>
    );
  },

  renderOther: function () {
    if (!this.state.toggled) {
      return null;
    }

    return (
      <TouchableHighlight onPress={this.handlePress} underlayColor="white">
        <ExampleComponent />
      </TouchableHighlight>
    );
  }
}) 

@a1exlism
Copy link

a1exlism commented Mar 8, 2017

@ThiagoNP Thanks for this demos, help me a lot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment