Categories
React Native

React Native — Touchables and Navigation

Spread the love

React Native is a mobile development that’s based on React that we can use to do mobile development.

In this article, we’ll look at how to use it to create an app with React Native.

TouchableNativeFeedback

The TouchableNativeFeedback component is available on Android to let us display an ink service reaction when we touch it.

For example, we can write:

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

export default function App() {
  return (
    <View style={styles.container}>
      <TouchableNativeFeedback
        onPress={() => {
          alert('You tapped the button!');
        }}
      >
        <View style={styles.button}>
          <Text style={styles.buttonText}>TouchableNativeFeedback</Text>
        </View>
      </TouchableNativeFeedback>
    </View >
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
  },
  button: {
    marginBottom: 30,
    width: 260,
    alignItems: 'center',
    backgroundColor: '#2196F3'
  },
  buttonText: {
    textAlign: 'center',
    padding: 20,
    color: 'white'
  }
});

to add the TouchableNativeFeedback component with content inside.

TouchableOpacity

The TouchableOpacity component is used to provide feedback by reducing the opacity of the button.

The background will be seen through it while the user presses it down.

For instance, we can write:

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

export default function App() {
  return (
    <View style={styles.container}>
      <TouchableOpacity
        onPress={() => {
          alert('You tapped the button!');
        }}
      >
        <View style={styles.button}>
          <Text style={styles.buttonText}>TouchableOpacity </Text>
        </View>
      </TouchableOpacity >
    </View >
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
  },
  button: {
    marginBottom: 30,
    width: 260,
    alignItems: 'center',
    backgroundColor: '#2196F3'
  },
  buttonText: {
    textAlign: 'center',
    padding: 20,
    color: 'white'
  }
});

to add the TouchableOpacity component in our app.

TouchableWithoutFeedback

The TouchableWithoutFeedback component lets us add a touchable component without feedback.

For example, we can write:

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

export default function App() {
  return (
    <View style={styles.container}>
      <TouchableWithoutFeedback
        onPress={() => {
          alert('You tapped the button!');
        }}
      >
        <View style={styles.button}>
          <Text style={styles.buttonText}>TouchableWithoutFeedback </Text>
        </View>
      </TouchableWithoutFeedback >
    </View >
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
  },
  button: {
    marginBottom: 30,
    width: 260,
    alignItems: 'center',
    backgroundColor: '#2196F3'
  },
  buttonText: {
    textAlign: 'center',
    padding: 20,
    color: 'white'
  }
});

We add the TouchableWithoutFeedback component to add a button that doesn’t have any feedback when we touch it.

Navigating Between Screens

Mobile apps usually are made of multiple screens.

Therefore, we need a way to navigate between multiple screens.

React Native lets us do this with the React Navigation library.

To install the dependencies, we first run:

npm install @react-navigation/native @react-navigation/stack

Then we install the peer dependencies in our Expo manged project by running:

expo install react-native-reanimated react-native-gesture-handler react-native-screens react-native-safe-area-context @react-native-community/masked-view

Then we can use the dependencies to add navigation by writing:

import React from 'react';
import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
import { Button, StyleSheet, Text, View } from 'react-native';

const Stack = createStackNavigator();

const HomeScreen = ({ navigation }) => {
  return (
    <View style={styles.container}>
      <Text> Home</Text>
      <Button
        title="Go to Profile"
        onPress={() =>
          navigation.navigate('Profile', { name: 'Jane' })
        }
      />
    </View>
  )
}

const ProfileScreen = ({ navigation }) => {
  return (
    <View style={styles.container}>
      <Text>Profile</Text>
      <Button
        title="Go to Home"
        onPress={() =>
          navigation.navigate('Home')
        }
      />
    </View>
  )
}

export default function App() {
  return (
    <NavigationContainer>
      <Stack.Navigator>
        <Stack.Screen
          name="Home"
          component={HomeScreen}
          options={{ title: 'Welcome' }}
        />
        <Stack.Screen name="Profile" component={ProfileScreen} />
      </Stack.Navigator>
    </NavigationContainer>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
  },
});

We have 2 components HomeScreen and Profile screen which are used for our app’s screens.

It has the navigation prop that lets us call the navigate method to navigate to the page we want.

In App , we add the NavigationContainer to add navigation.

Stack.Navigator is a container for the navigable components.

Stack.Screen adds our screens.

The component prop has the component to show.

Conclusion

We can add touchable components to add touchable items that has our own content.

Also, we can add navigation with various components.

Leave a Reply

Your email address will not be published. Required fields are marked *