Creating a seamless design experience in React Native by incorporating inline images along with

Seeking guidance on creating a unique layout in React Native.

I aim to display inline sentences with images acting as separators, akin to commas in a sentence...

I attempted to use a JSON file containing an array, implementing a map function to include text and image tags.

Unfortunately, the output did not align with my intended design.

https://i.sstatic.net/yBr5J.jpg

import React, { Component } from 'react';
import image1 from './assets/image.jpg'
import {
  StyleSheet,
  ScrollView,
  View,
  Text,
  Image,
} from 'react-native';
import Monday1 from "./data/monday1";
class App extends Component {
  constructor(props) {
    super(props);
  }
  render() {
    return (
      <ScrollView>
        <View style={{ marginRight: 20, marginLeft: 20, marginTop: 20 }}>
          <View style={{ flex: 1, flexDirection: 'row', flexWrap: 'wrap' }}>
            {Monday1.map((Monday1Text, key) =>
              <View style={styles.border2}>
                <View style={styles.border}>
                  <Text key={key} style={styles.text}>
                    {Monday1Text}
                  </Text>
                </View>
                <Image style={{ width: 50, height: 50, borderRadius: 50 }} source={{ uri: 'https://www.theladders.com/wp-content/uploads/Lion_030818-800x450.jpg' }} ></Image>
              </View>
            )}
          </View>
        </View>
      </ScrollView>
    )
  };
}

const styles = StyleSheet.create({
  border: {
    borderColor: 'red',
    borderStyle: 'solid',
    borderWidth: 2,
  },
  border2: {
    borderColor: 'green',
    borderStyle: 'solid',
    borderWidth: 5,
  },
  text: {
    fontSize: 22,
  },
  image: {
    overflow: 'hidden',
    ...Platform.select({
      android: {
        borderRadius: 25
      }
    }),
    height: 50,
    margin: 20,
    width: 50,
  },
});
export default App;

Your insights and assistance on this matter would be greatly appreciated. Thank you :)

Answer №1

React Native has built-in support for displaying inline images within text. This functionality was introduced in a recent commit, allowing developers to easily insert images directly into the Text component for seamless integration.

Here's an example:

<Text>
  <Image
    style={{
      width: 20,
      height: 20,
      borderRadius: 10,
    }}
    source={require('../../static/user-ico.png')} 
  />
   Adding comments like this enhances the readability of your code. Feel free to experiment with different text and image combinations to see how they render together.
</Text>

https://i.sstatic.net/MfbRd.png

Furthermore, you can mix and match Text components with Images as needed for your specific project requirements.

P.S. Tested successfully on android and RNWeb platforms.

Answer №2

To optimize the layout, consider applying

{ flexDirection: 'row', flexWrap: 'wrap' }
to the
<View style={styles.border2}>
. Be sure to individually wrap each word (not just blocks of words) in a Text component and adjust the lineHeight to match that of the Image.

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

Adding and removing dynamic fields with Bootstrap functionality

Recently, I've been trying to develop a feature where users can add and remove fields by clicking on a button. However, I've encountered a roadblock in my progress. If you take a look at this CodePen link, you'll see what I have so far. My a ...

Breaking the Boundaries of Fuzzy Search with JavaScript

I am currently utilizing ListJS's plugin for fuzzy search. It can be found at the following link: In an attempt to enhance the plugin, I implemented my own method to filter by the first letter of the items in the list. This involved selecting from an ...

Programmatically loading a URL into an ng-view component

Given the code snippet below, I am looking to dynamically load the URL for the `viewTeam` page into the `ng-view` container upon calling the `showTeam()` function. What would be the best approach to achieve this functionality? <html> <head> ...

What is the reason for needing a page reload in Javascript or JQuery?

I'm curious why Javascript or jQuery require a page reload before applying certain effects. CSS updates in real-time, as demonstrated by the following example: This changes dynamically without needing to refresh the page @media all and (max-width:7 ...

In ReactJS, the decision to show either an onclick event or a Link component depends on specific logic that

My objective is to have different HTML displayed with onclick compared to route I am struggling with the logic, as I don't want it in the Render() method. const contents = this.state.data.map(item => ( This is where my struggle lies <button i ...

Utilize fileSystem in an API Route when working with NextJS deployed on Vercel

I am facing an issue with using fileSystem methods like readdirSync on an API Route in NextJS. It functions properly locally but returns a 500 status code when deployed on Vercel. Here is the error message from Vercel's Function Logs: catch Error: EN ...

Is it appropriate for HTML5 Web Workers to utilize CORS for cross-origin requests?

As I was creating a hosted API that relies on web workers, I encountered an intriguing issue. I am looking for feedback from the community to help me with this. Even though my server has the necessary CORS headers in place to serve the worker JS files and ...

Establish a distinct perspective

In the process of developing a web application, I am using the MVC pattern to organize the client-side javascript. Each page has its own view connecting to a central model. I want the views to be able to operate independently without any knowledge of each ...

Saving two different values in React's state using Typescript

I am currently working on a form that consists of three number fields. The user is able to input a quantity and unit price, which will then calculate and display the total price in the last field as a disabled input. Although my current code successfully ...

What are the methods for differentiating between a deliberate user click and a click triggered by JavaScript?

Social media platforms like Facebook and Twitter offer buttons such as Like and Follow to allow users to easily engage with content. For example, on Mashable.com, there is a Follow button that, when clicked, automatically makes the user follow Mashable&ap ...

Individual files dedicated to each controller in Angular.js are a common practice in development

In my project, I am looking to create an Angular module with a main controller, as well as additional controllers that are stored in separate files. Main module: var app = angular.module("main", []); app.controller("mainCtrl", function ($scope) { $scop ...

Tips for creating a for loop in a .js script within MongoDB that allows for passing a variable containing the database name to a text file

In a .txt file, I have a list of database names as shown below: local test admin Is there a way to dynamically pass arguments instead of hardcoding them in .js scripts for mono go? db = db.getSiblingDB('test'); date = new Date() dat ...

exploring CORS settings in fetch API for secure GET requests

My endpoint is localhost:8080/enquiry and it returns the following JSON: [{"_id":"5a283e4c5a36f4556af34742", "firstName":"bob", "surname":"hoskins", "telephoneNumber":939483948, "gender":"male", "dayOfBirth":17, "monthOfBirth":5, "ye ...

Issue with Chrome when setting background color to #333223 using the .style method

This code snippet demonstrates the toggling of a div on and off. The use of .style property controls the background color of the parent element when toggling the div. Interestingly, the code works flawlessly in Opera but seems to have issues in Chrome. Des ...

Convert JSON data into an HTML table using JavaScript without displaying the final result

I need help troubleshooting my JavaScript code that is supposed to populate an HTML table with data from a JSON file. However, the table remains empty and I can't figure out why. Sample JSON Data [{"User_Name":"John Doe","score":"10","team":"1"}, { ...

Reordering images using jQuery leads to a malfunctioning reloading of images

I am in search of the most effective approach to alter the src attribute for multiple images on a page post-loading. Essentially, I need to reorganize them for better display. In their initial arrangement, they are presented as follows: 1 5 9 2 ...

Extracting a targeted subsequence from a string in JavaScript

I have a dynamic string that is created in one of the following ways: var q = "FROM Table SELECT sum(1), count(2), avg(3) where x='y' var q = "SELECT min(1), max(2), average(3) FROM Table where z='x' since y days ago The va ...

Ways to activate a click event on a parent div without affecting a particular child element in Angular

I am working on an Angular component that consists of nested div elements. My goal is to trigger a click event on the parent div, however, I want to ensure that if the menu child div is clicked, the parent div's click event should not be triggered. C ...

Having trouble with Google font displaying on React site but not on mobile devices?

After importing a Google font that appears perfectly on desktop and cross-browser, an issue arises on mobile where default fonts are shown instead. Below is a snippet from my App.css file: @import url("https://fonts.googleapis.com/css2?family=Turret+Ro ...

"Encountering issues with supertest due to connection reset error (

Currently, I am tackling a Node.js project and endeavoring to craft test cases for the application. Oddly enough, the endpoints function seamlessly when executed in Postman. However, upon running the test cases, peculiarly, some of them - typically the ini ...