How to add a button in React Native?

后端 未结 9 672
暖寄归人
暖寄归人 2021-01-31 07:46

I´m confused with this whole \"no CSS\" thing, but I understand why it\'s beneficial. All I want to do is place a button in the middle of the screen but I don\'t understand how

相关标签:
9条回答
  • 2021-01-31 08:19

    You have two options to achieve a touchable component/button to handle user's events.

    • One is to use the built in Button Component. Check the docs here http://facebook.github.io/react-native/docs/button.html
    • Two use either TouchableHighlight or TouchableNativeFeedback or TouchableOpacity or TouchableWithoutFeedback. Think of this as a way for you to convert different areas of your app to tappable(clickable) or a way for you to create a custom button. Each component here is different based on how it behaves once it's tapped by the user. Check the docs for more details. http://facebook.github.io/react-native/docs/touchablewithoutfeedback.html etc.

    Concerning styling in react native you will need to understand flexbox layout. Check this css flexbox article all rules are applicable to react-native https://css-tricks.com/snippets/css/a-guide-to-flexbox/ except that you will have to capitalize the rules e.g align-content to alignContent

    0 讨论(0)
  • 2021-01-31 08:20

    Update: use built-in Button component.

    Deprecated:

    Wrap your View into TouchableHighlight for iOS and TouchableNativeFeedback for Android.

    var {
      Platform,
      TouchableHighlight,
      TouchableNativeFeedback 
    } = React;
    
    var tapSpeed = React.createClass({
      buttonClicked: function() {
        console.log('button clicked');
      },
      render: function() {
        var TouchableElement = TouchableHighlight;
        if (Platform.OS === 'android') {
         TouchableElement = TouchableNativeFeedback;
        }
        return (
        <View style={styles.container}>
          <Text style={styles.welcome}>
            Tap me as fast as you can!
          </Text>
          <TouchableElement
            style={styles.button}
            onPress={this.buttonClicked.bind(this)}>
            <View>
              <Text style={styles.buttonText}>Button!</Text>
            </View>
          </TouchableElement>        
        </View>
        );
      }
    });       
    
    0 讨论(0)
  • 2021-01-31 08:22
    <Button
      onPress={onPressLearnMore}
      title="Learn More"
      color="#841584"
      accessibilityLabel="Learn more about this purple button"
    />
    
    0 讨论(0)
  • 2021-01-31 08:26

    Please check react-native doc's regarding the buttons

    You have more than one way to add button in your application and styling it

    You can use Button tag and it's have only one way styling by color attribute, it will appearing in IOS different than Android, or by putting button in view tag with style

    <View style={style.buttonViewStyle}> <Button title="Facebook" color="blue" onPress={this.onFacebookPress} /> </View>

    And check the TouchableOpacity and TouchableNativeFeedback tags

    And take a lock on below link for more options to add custom buttons in your app

    https://js.coach/react-native/react-native-action-button?search=button

    0 讨论(0)
  • 2021-01-31 08:28

    The Button element from react-native package does not provide built in styling feature. Ex. The "title" props will be in upper case by default. Hence, I've used an another package react-native-elements that provides nice features for Button element along with different styling options.

    You can refer more details on Button from react-native-elements

    0 讨论(0)
  • 2021-01-31 08:31
    import React, { Component } from 'react';
    import { StyleSheet, View, TouchableOpacity, Text} from 'react-native';
    var tapSpeed = React.createClass({
      render: function() {
        return (
          <View style={styles.container}>
            <TouchableOpacity>
              <Text style={styles.welcome}>
                Tap me as fast as you can!
              </Text>
            </TouchableOpacity>
            <TouchableOpacity style={styles.button}>
              <Text>!</Text>
            </TouchableOpacity>
          </View>
        );
      }
    });
    
    var styles = StyleSheet.create({
      container: {
        flex: 1,
        justifyContent: 'center',
        flexDirection: 'column',
        alignItems: 'center',
        backgroundColor: '#FFCCCC'
      },
      welcome: {
        fontSize: 20,
        textAlign: 'center',
        margin: 10,
        alignSelf: 'center'
      },
      button: {
        justifyContent: 'center',
        alignItems: 'center',
        marginBottom: 7,
        border: 1px solid blue,
        borderRadius: 2px
      }
    });
    
    
    0 讨论(0)
提交回复
热议问题