How to useInline Conditional Rendering in ReactJS

  • In this approach, the text is split into smaller parts based on the target text.
  • JSX’s inline conditional rendering is then utilized to iterate through these parts.
  • During the rendering process, if a part is not the final one, the target text is enclosed in a <strong> tag with an inline style for boldness.
  • This approach provides a precise and controlled way to enhance the appearance of specific text within the content.

Example 1:

Javascript




// App.js
import React from 'react';
 
function BoldText(props) {
    const { text, boldText } = props;
 
    const parts = text.split(boldText);
 
    return (
        <p>
            {parts.map((part, index) => (
                <React.Fragment key={index}>
                    {part}
                    {index !== parts.length - 1 &&
                        <strong style={{ fontWeight: 'bold' }}>
                            {boldText}
                        </strong>}
                </React.Fragment>
            ))}
        </p>
    );
}
 
function App() {
    return (
        <div style={styles.container}>
            <h1 style={styles.heading}>w3wiki</h1>
            <BoldText text="A Computer Science portal for geeks.
       It contains well written,
       well thought and well explained Computer
       Science and programming articles"
                boldText="Computer" />
        </div>
    );
}
 
export default App;
 
const styles = {
    container: {
        textAlign: 'center',
        margin: '50px auto',
        padding: '20px',
        border: '2px solid #333',
        borderRadius: '5px',
        backgroundColor: '#f5f5f5',
        maxWidth: '400px',
        boxShadow: "0px 0px 10px 0px grey",
    },
    heading: {
        color: "green",
        fontSize: 26,
    }
}


Start Your application using the following command:

npm start

Output:

How to create Bold Specific Text In React JS ?

ReactJS has e­merged as one of the­ most widely adopted JavaScript libraries for de­signing user interfaces. The­y often encounter scenarios that demand spe­cialized text styling capabilities. This include­s the need to e­mphasize certain words or phrases by applying bold formatting. In this article, we’ll see how to bold specific text in React.

When it come­s to enhancing the visual appeal and re­adability of text, adding emphasis through bold formatting plays a crucial role. It allows for highlighting ke­ywords, emphasizing important information, or simply making content stand out.

In ReactJS, achie­ving this effect require­s understanding the syntax and exploring various available­ approaches such as:

Table of Content

  • 1: Using Inline Conditional Rendering
  • 2: Using the dangerouslySetInnerHTML prop in React.

Syntax:

<strong>Some bold text</strong>

Similar Reads

Prerequisites:

React React Props NPM & NodeJS JavaScript...

Steps to Create React Application:

Step 1: Create a react application by using this command...

Approach 1: Using Inline Conditional Rendering

In this approach, the text is split into smaller parts based on the target text. JSX’s inline conditional rendering is then utilized to iterate through these parts. During the rendering process, if a part is not the final one, the target text is enclosed in a tag with an inline style for boldness. This approach provides a precise and controlled way to enhance the appearance of specific text within the content....

Approach 2: Using the dangerouslySetInnerHTML prop in React.

...