Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't find variable: Button - React Native

Tags:

react-native

I am trying to create a simple app with a button in react native, but so far every time I run my code, it gives me an error saying "Can't find variable: Button". I would like to end up with my title at the top (Already done) and a button in the center of the screen, which gives an alert when touched.

I have run through several online tutorials and cannot find a solution.

Here is my code:

        /**
     * Sample React Native App
     * https://github.com/facebook/react-native
     * @flow
     */

    import React, { Component } from 'react';
    import {
      AppRegistry,
      StyleSheet,
      Text,
      View
    } from 'react-native';

    class Project extends Component {
      render() {
        return (
          <View style={styles.container}>
           <Text style={styles.Title}>
             Lucas's App
            </Text>
           <View style={styles.buttonContainer}>
             <Button
               onPress={() => { Alert.alert('You tapped the button!')}}
               title="Press Me"
             />
            </View>
          </View>
        );
      }
    }

    const styles = StyleSheet.create({
      container: {
        flex: 1,
        backgroundColor: '#F5FCFF',
        },
      Title: {
        color: '#000000',
        marginTop: 13,
        paddingLeft:100,
        paddingRight:100,
        fontFamily: 'Avenir',
        fontSize: 30,
      },
        buttonContainer: {
        margin: 20
      },

    });

    AppRegistry.registerComponent('Project', () => Project);

Thanks in advance.

like image 796
Lucas Farleigh Avatar asked Oct 13 '17 23:10

Lucas Farleigh


People also ask

How do I create a custom button in React Native?

To create custom buttons, you need to customize the <TouchableOpacity /> component and include the <Text /> component inside of it to display the button text. const AppButton = ({ onPress, title }) => ( <TouchableOpacity onPress={onPress} style={styles. appButtonContainer}> <Text style={styles.


1 Answers

You need to make sure to import the button. You can do this by adding this before your code:

 import {
  AppRegistry,
  StyleSheet,
  Text,
  View,
  Button,
  Alert
} from 'react-native';

[Or just add button to the import list]

like image 157
Lucas Farleigh Avatar answered Nov 06 '22 20:11

Lucas Farleigh