Dynamically Highlighting Text in React Native

Dynamically Highlighting Text in React Native

Or… How I learned to love React FlowBox.

This one should have been easy — or so I thought. I had a block of text, and I needed to highlight text in that block. I figured I’d whip something together, and be done in a few minutes. Then, I started WAY overthinking things.

Let’s walk through all the things I did wrong. :)

First — here’s my stated output:

Image for post

Actually, ‘var1’ is a clickable pressable box.

The text came from this:

“text”: “Carefully, ‘{var var1}’ you say to the Grue! “

This is a JSON file containing the data I’m reading in. It could just as easily be a REST service or other item.

I’ve defined a pseudo-grammar to allow me to specify variables and other actions in the text, which allow me to control the program flow, none of which is the point of this article.

What I want to do is to highlight the var1 in that sentence, ensuring that it stands out to the user.

This sentence, if it was not dynamic, could be composed of the following:

     <Text> Hello 
          onPress={() => {
            Hello <Text style={{color: 'red'}}> you again </Text>

And that would have been all well and good.

reactjs javascript react react-native

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

How native is React Native? | React Native vs Native App Development

Article covers: How native is react native?, React Native vs (Ionic, Cordova), Similarities and difference between React Native and Native App Development.

Hire Dedicated React Native Developer

Have you ever thought of having your own app that runs smoothly over multiple platforms? React Native is an open-source cross-platform mobile application framework which is a great option to create mobile apps for both Android and iOS. **[Hire...

React Native Mobile App Development

Skenix is providing React Native Development Services with qualified React Native App Developers. Get the best React Native App Development Services.

React Native Redux | How To Use Redux In React Native

React Native Redux Example Tutorial From Scratch. We connect the React Native app to Redux store to save and retrieve the application data.

Build a simple React Native Pokemon app with React-Navigation

As we start learning new technologies we want to start building something or work on a simple project to get a better understanding of the technology.