How to add a button in React Native?

后端 未结 9 675
暖寄归人
暖寄归人 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:32
    export default class Login extends React.Component {
      barcodeAction = () => {
        this.props.navigation.navigate('BarCodeScanner')
      }
    
      cleverTapAction = () => {
        this.props.navigation.navigate('CleverTapApp')
      }
    } 
    
    render() {
        return (
          <View style={styles.container}>
            <View style={styles.buttonContainer}>
              <Button
                onPress={this._onPressButton}
                title="Press Me"
              />
            </View>
            <View style={styles.buttonContainer}>
              <Button
                onPress={this._onPressButton}
                title="Press Me"
                color="#841584"
              />
            </View>
            <View style={styles.alternativeLayoutButtonContainer}>
              <Button
                onPress={this._onPressButton}
                title="This looks great!"
              />
              <Button
                onPress={this._onPressButton}
                title="OK!"
                color="#841584"
              />
            </View>
          </View>
        );
      }
    }
    
    const styles = StyleSheet.create({
      container: {
       flex: 1,
       justifyContent: 'center',
      },
      buttonContainer: {
        margin: 20
      },
      alternativeLayoutButtonContainer: {
        margin: 20,
        flexDirection: 'row',
        justifyContent: 'space-between'
      }
    });
    

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

    You can use built in react-native Button element.

    import React, { Component } from 'react';
    import { StyleSheet, View, Button, Alert, AppRegistry } from 'react-native';
    
    class MainApp extends Component {
    
    _onPress() {
      Alert.alert('on Press!');
     }
    
      render() {
        return (
          <View style={styles.container}>
            <View style={styles.buttonContainer}>
              <Button onPress={this._onPress} title="Hello" color="#FFFFFF" accessibilityLabel="Tap on Me"/>
            </View>
          </View>
        );
      }
    }
    
    const styles = StyleSheet.create({
      container: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
        backgroundColor: '#FFFFFF'
      },
      buttonContainer: {
        backgroundColor: '#2E9298',
        borderRadius: 10,
        padding: 10,
        shadowColor: '#000000',
        shadowOffset: {
          width: 0,
          height: 3
        },
        shadowRadius: 10,
        shadowOpacity: 0.25
      }
    })
    
    AppRegistry.registerComponent('MainApp', () => MainApp);
    

    Read More Here.

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

    The react-native-button package provides a button that is styled like a native button. Install it with npm install react-native-button and use it in your component like this:

    var Button = require('react-native-button');
    
    var ExampleComponent = React.createClass({
      render() {
        return (
          <Button
            style={{borderWidth: 1, borderColor: 'blue'}}
            onPress={this._handlePress}>
            Press Me!
          </Button>
        );
      },
    
      _handlePress(event) {
        console.log('Pressed!');
      },
    });
    
    0 讨论(0)
提交回复
热议问题