How to Set a Button as Inline Text in a React Native Component

Below is the code snippet I am working with utilizing the Text and Button components provided by react-native-paper:

<Text>See also </Text>
<Button mode="text" compact onPress={this.nav( name )}>Compass</Button>
<Text> on how to use the Compass.</Text>

When this is displayed, it looks like:

https://i.stack.imgur.com/lWpub.png

If I swap out the Button for TouchableOpacity or a similar component, the layout changes to this:

https://i.stack.imgur.com/EEgBF.png

Is there a way to adjust the styling of the Button or TouchableOpacity so that it aligns better with the surrounding text?

UPDATE

After implementing the solution provided by @RajendranNadar and making adjustments to make it compatible with android devices:

See also <Pressable onPress={() => alert('Hello :)')}><Text style={styles.text}>Compass</Text></Pressable> on how to use the Compass.

The outcome is as shown below:

https://i.stack.imgur.com/epjzR.png

Answer №1

If you want to achieve this goal, consider utilizing nested elements in your text. You can try the following approach:

const NestedTextComponent = () => {
  return (
    <Text>Check out <Text style={styles.link} onPress={() => {}}>this link</Text> for more information on how to implement nested texts.</Text>
  );
};

const styles = StyleSheet.create({
  link: {
    color: 'blue',
    textDecorationLine: 'underline'
  }
});

Answer №2

If you're looking for the most effective way to utilize the Pressable element within the react-native library, this is it!

<Text>
  For more information on how to implement the Compass feature, take a look at <Pressable onPress={() => alert('Hello')}>Compass</Pressable>.
</Text>

To see a demonstration in action, visit the following link:

For further details on the Pressable component, check out the documentation here: Pressable component docs

Answer №3

this is a complete example that works perfectly.

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

const PrivacyPolicy = ({title = 'GET STARTED'}) => {
  const openUrl = async () => {
    let url = 'https://facebook.com';
    await Linking.canOpenURL(url)
      .then(() => Linking.openURL(url))
      .catch(err => {
        console.log(err);
      });
  };
  return (
    <View style={styles.container}>
      <Text style={styles.text}>
        By Clicking The "{title}" Button, You Agree to Our
        <TouchableWithoutFeedback onPress={() => openUrl()}>
          <Text style={styles.text2}> Privacy Policy </Text>
        </TouchableWithoutFeedback>
        And
        <TouchableWithoutFeedback onPress={() => openUrl()}>
          <Text style={{...styles.text2}}> End User License Agreement</Text>
        </TouchableWithoutFeedback>
      </Text>
    </View>
  );
};

export default PrivacyPolicy;

const styles = StyleSheet.create({
  container: {
    width: SCREEN_WIDTH - moderateScale(20),
    alignSelf: 'center',
    marginTop: moderateScale(10),
  },
  text: {
   
    includeFontPadding: false,
    fontSize: moderateScale(12),
  },
  text2: {
   
    fontSize: moderateScale(12),
    includeFontPadding: false,
  },
});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Managing both clicking and hovering events on a single element, ensuring that the popup modal remains open as long as it is being hovered over

After successfully implementing click and hover functionality on an element, I was able to position the popup relative to the mouse pointer based on a previous solution. However, I am now facing an issue where I want the popup modal to be fixed in a specif ...

Two tables are positioned using distinct alignments but share a common stylesheet

I have created two tables, one using HTML (with a touch of PHP) and the other with two PHP loops. Starting with the stylesheet: .statistics { font-family: Arial, monospace; font-size: 36px; margin-left: 5%; width: 90%; } .statistics tr { ...

Determining the height of a different element using only CSS, Stylus, or Nib

Looking to create a navigation bar using CSS Markup: <nav> Navigation content </nav> <div id="mainContent"> Main page content </div> CSS: nav { position:fixed; height: X%; min-height: Ypx; } #mainContent { ...

Creating a visually appealing webpage by utilizing Bootstrap and HTML for the layout

I am in the process of learning html and Bootstrap for CSS. Below is the code I currently have: <div class="container"> <div class="row text-center mb-4 mt-2"> <div class="col-md-12"> <div cla ...

Using CSS to mask images with border-radius

I am currently designing a website for a friend, and I have encountered an issue with an image that I have masked using CSS border-radius. The image is larger and taller than its container, so I used JavaScript to center it correctly. However, it appears ...

Automatically scrolling a div to the bottom in a React component

I am currently working on developing a chat application for my school project, and I would like to implement the feature that automatically scrolls to the bottom of the chat window. Despite trying various solutions and encountering React errors, I have not ...

Encountered a problem locating the "./aws-exports" file in the "App.js" directory

For a while now, I have been working on developing an application using awsamplify. Today, I decided to conduct some tests and when I executed npm run start-web Everything ran smoothly. Next, I proceeded to test the mobile version using Expo by running ...

Adjustable Greybox Overlay Resizes Window Dimensions

Utilizing greybox on my business website allows users to easily log in and access their accounts. View an example of my site However, when the login or sign up window is activated at the top of the page, the page or window size changes, causing scroll bar ...

Creating triangular shapes that can be interacted with by hovering

I'm trying to create a diagonal triangle on the screen that changes color when hovered over. However, my current method only creates a 'hack triangle' which is actually just a rectangle and doesn't respond to the transparent section. I ...

Switch the input direction from left to right

Check out my demonstration that is currently functional: JSFIDDLE $('#btn-search').on('click', function(e) { e.preventDefault(); $('#search').animate({width: 'toggle'}).focus(); }); Is there a way to modify ...

Items that share identical height and margin values will appear with divergent visual presentations

I am trying to align three horizontal lines so that they are the same height and have an equal spacing between each other. However, due to variations in height and margins, some lines appear larger or smaller than others. How can I ensure they all have th ...

Bone Structure Styles - adaptable form

I'm currently working on a form using skeleton css and initially set it up with a three-column layout. However, I found that this created too much white space due to varying content lengths in each column. I would like to switch to a horizontal layout ...

Issue with conflicting peer dependency: React Native for iOS

Struggling to build a React Native program on Mac by following the official website guidelines. Encountering errors during the process. Any advice on how to debug this? When trying "npx react-native init A2," prompted to install necessary packages but fa ...

Tips for making a range slider in react-native

Check out my range slider I'm having trouble with changing values by clicking on them. Is there a way to enable sliding the range slider instead? Thanks for any help. ...

Issue with CSS Transform Causing Rendering Errors in Both Chrome and Firefox

While a pixel here or there can be overlooked, in this case, precision is essential for the perfect rendering of this simple menu. The spacing between menu items must be impeccably equal to avoid any merging or disjointed appearance. Currently, the sep ...

Event listener is failing to execute the functions

Even though the inline onmouseover="verdadero()" function is properly set up Upon further inspection, it appears that while the event listener is added to the box element, the function is not being triggered when hovering over it, and console.lo ...

Error in react-native while attempting to find the lowest common ancestor of `responderInst` and `targetInst` using `Event

After upgrading my react-native app to version 0.44, I encountered an issue where the app would start up without any problems in both the simulator and on a mobile device. However, when I tried pressing a component like a button or widget, a red error scre ...

When an absolute positioned element exceeds the right border of its relative parent, its width will be truncated

When position: absolute; divs are placed inside or around a position: relative; parent, their height and width are based on the size and length of the text they contain. However, if one of these divs extends beyond the right border of the relative parent, ...

Ways to showcase INPUT TYPE when making a Selection?

I've been struggling with a simple issue and despite trying multiple solutions, I can't seem to get it right. I have a form where I'm using the <select> tag with two options: coo and uh. What I want is for an additional input type fiel ...

What is the best way to maintain scrollbars on mobile devices?

I'm currently building a cross-platform application using Angular 4 that needs to work seamlessly on both mobile and desktop devices. Is there a special trick to prevent the scrollbars from disappearing when viewing this page on a mobile browser? I&ap ...