concept button in category react native

appears as: buttons, button, buttons, The button
React Native in Action

This is an excerpt from Manning's book React Native in Action.

Components are the fundamental building blocks of React Native, and they can vary in functionality and type. Examples of components in popular use cases include buttons, headers, footers, and navigation components. They can vary in type from an entire view, complete with its own state and functionality, to a single stateless component that receives all its props from its parent.

The app will display a heading, a text input, a button, and a tab bar. When you add a todo, the app will add it to the array of todos and display the new todo beneath the input. Each todo will have two buttons: Done and Delete. The Done button will mark it as complete, and the Delete button will remove it from the array of todos. At the bottom of the screen, the tab bar will filter the todos based on whether they’re complete or still active.

Now that the submitTodo function has been created, create a file called Button.js and wire up the function to work with the button.

Listing 3.12 Creating the Button component
import React from 'react'
import { View, Text, StyleSheet, TouchableHighlight } from 'react-native'

const Button = ({ submitTodo }) => (    #1  
  <View style={styles.buttonContainer}>
    <TouchableHighlight
      underlayColor='#efefef'
      style={styles.button}
      onPress={submitTodo}>    #2  
      <Text style={styles.submit}>
        Submit
      </Text>
    </TouchableHighlight>
  </View>
)

const styles = StyleSheet.create({
  buttonContainer: {
    alignItems: 'flex-end'
  },
  button: {
    height: 50,
    paddingLeft: 20,
    paddingRight: 20,
    backgroundColor: '#ffffff',
    width: 200,
    marginRight: 20,
    marginTop: 15,
    borderWidth: 1,
    borderColor: 'rgba(0,0,0,.1)',
    justifyContent: 'center',
    alignItems: 'center'
  },
  submit: {
    color: '#666666',
    fontWeight: '600'
  }
})

export default Button

#1   Destructures the submitTodo function, which was passed as a prop to the component
#2   Attaches submitTodo to the onPress function available to the TouchableHighlight component. This function will be called when the TouchableHighlight is touched or pressed.

In this component, you use TouchableHighlight for the first time. TouchableHighlight is one of the ways you can create buttons in React Native and is fundamentally comparable to the HTML button element.

Figure 4.2 A simple application that supports two themes, white and black. Users can press the button to toggle between a white background and a black background.

c04_02.eps