Is it possible to reorganize React Components based on a variable?

I am working with a React Component that is rendered using the map function below:

<div className="links-container">
    {links.map((link, i) => (
        <Links
            key={link.text}
            icon={link.icon}
            text={link.text}
            isRight={i % 2 === 0 ? true : false}
        />
    ))}
</div>
import React, { Component } from "react";

export default class Links extends Component {
    render() {
        const { icon, text, isRight } = this.props;
        return (
            <div style={{ alignSelf: isRight ? "flex-end" : "" }}>
                <div className="link">
                    <img
                        className="link-img"
                        src={icon}
                        alt="link"
                        style={{ borderColor: isRight ? "#1689FC" : "#FD003A" }}
                    />
                    <div className="link-text">{text}</div>
                </div>
            </div>
        );
    }
}

My goal is to change the order in which the image and text are rendered based on the value of isRight. I want the text to be rendered first if isRight is true, and the image to be rendered first if isRight is false. Currently, I am using an if statement with repetitive code like this:

isRight ? <div><text/><img/></div> :  <div><img/><text/></div>

However, I am exploring alternative solutions as my current approach involves redundant code, which defeats the purpose of having the Links Component in the first place.

Answer №1

Utilize the display:flex and flex-direction attributes within the <div className="link">

Choose between flex-direction: row-reverse or flex-direction: column-reverse based on your particular design.

Answer №2

If you want to change the direction of your flexbox items, consider trying out this one-liner: flex-direction: row-reverse

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

Interested in creating a Twitter bot that can automatically share images from a designated folder in sequential order. Leveraging the power of node.js

Recently, I've been following an online guide to create a Twitter bot that tweets images from a folder on my computer. While I have a vast collection of photos I'd like to share, the guide only demonstrates how to post them in a random sequence. ...

What is the best way to ensure that this <span> maintains a consistent width, no matter what content is placed inside

So here's the deal, I've got some dynamically generated html going on where I'm assigning 1-6 scaled svgs as children of a . The span is inline with 2 other spans to give it that nice layout: https://i.sstatic.net/mySYe.png I want these "b ...

Mastering the correct way to access array index positions in Vuejs

HelloWorld.vue <template> <div> <div v-for="(pizza, index) in pizzas" :key="pizza.name"> {{ pizza.name }} <List :content="matchingPizza" :pname="pizza.name" :qname="quantities[index] ? ...

What is the process of transforming an object type into a two-dimensional array using lodash?

In order to properly display multiple tables in my Angular project, I am looking to convert an object type into an array of different objects. The object I am working with is as follows: let myObject = { internalValue:{city:"Paris", country:"France", pin ...

Guide to using the DOM inspector tool with a RAW HTML document

Utilizing PHP Simple HTML DOM Parser, I periodically extract information from various websites. I rely on Chrome's DOM inspector to analyze how to retrieve the desired data. An issue arose when one website (specifically TPB) had improper HTML code th ...

What is the process for creating distinct templates for various devices such as mobile phones, desktops, and tablets using Angular?

I am working on an Angular 9 application. Currently, I have some mockups but the template appears differently on mobile compared to desktop. However, the data fetched from API calls is mostly similar for both platforms, with slight variations possible be ...

Angular responsive button

Currently, I am working on an application using angular 8. In the app, there is a form that includes several buttons. I am looking for a solution to make these buttons responsive on mobile devices when viewed in Chrome and Firefox browsers. Here is the cur ...

The conversion of HTML entities such as € to its corresponding symbol does not occur during CSV conversion

I recently utilized a CSV parser from http://code.google.com/p/parsecsv-for-php/ to convert my reports into CSV format using PHP. The Sales Total value was displayed as &euro;XXXX.XX on both the browser and Excel (post-export), showcasing the Euro symb ...

Experiencing an issue with mui/material grid causing errors

An error occurred in the file Grid2.js within node_modules/@mui/material/Unstable_Grid2. The export 'createGrid' (imported as 'createGrid2') could not be found in '@mui/system/Unstable_Grid' as the module has no exports. Desp ...

I need to loop through an array of ids and add a new mongodb document for any ids that are missing. What is the best way to ensure that they are saved permanently?

Currently, I am utilizing Node JS and MongoDB to manage a collection of documents containing IDs ranging from 1 to 5000. Nevertheless, there are certain IDs that are absent, and I would like each document to be assigned one unique ID. Below is the snippet ...

The ReactNative FusionChart events in iOS have a limited emission/fire capability, with only the first event being able to emit

The events prop of FusionChart in react-native-fusioncharts does not seem to be functioning correctly. Only the first key in the event object will trigger. For example, only console.log("bI"); will be executed, while other events like onInitialized won&ap ...

Masonry is not compatible with Bootstrap 3

Can someone help me figure out how to make Masonry work with my Bootstrap grid columns? Step 1 - First, I included the Masonry script in my <head> like this : <script src="http://cdnjs.cloudflare.com/ajax/libs/masonry/3.1.5/masonry.pkgd.min.js"& ...

What is the best way to insert a text input with variable width into a table?

I am currently using twitter-bootstrap and have encountered an issue with two tables. The first table can be viewed here: http://jsfiddle.net/MELUd/ <table class="table table-bordered"> <thead> <tr> <td colspan ...

Balancing columns with active elements to ensure equal resizing

I am working on coding an image that requires specific resizing capabilities: https://i.sstatic.net/7hLwL.png The objective is to ensure that when the browser window is resized, the center notch remains proportional and both columns scale uniformly. Bel ...

What is the best way to vertically align Bootstrap Columns to ensure they have consistent heights?

I'm currently in the process of building a website that features multiple images and content organized into columns using Bootstrap as the framework. I'm wondering if there's a way to automatically adjust all the columns to the height of th ...

The performance of Noble BLE in Node.js can be inconsistent at times, with occasional successes and failures

Whenever I try to run the code below, I keep getting different outcomes each time. Sometimes it just shows "Powered Off". Other times, it displays both "Powered on" and "Scanning started". And occasionally, it goes all the way through giving multiple "stil ...

Display the html content within a <div> element by leveraging the power of jQuery

Can someone help me with displaying the HTML contents from a dropdown menu button link to a <DIV>? I would appreciate if you could provide a sample page for reference. Thank you in advance :) ...

Using Angular 2 to implement dynamic variables within HTML

I am struggling with a list that has the following structure: <ul *ngFor="#item of items"> <li><img src="http://something.com/[this needs to be item.id]/picture"></li> </ul> The challenge I am facing is getting the image URL ...

Unable to find the locally stored directory in the device's file system using Nativescript file-system

While working on creating an audio file, everything seems to be running smoothly as the recording indicator shows no errors. However, once the app generates the directory, I am unable to locate it in the local storage. The code I am using is: var audioFo ...

Styling Vue JS Components with CSS Binding

I've been trying to apply CSS styling to vuejs tags without success. I'm struggling to get it to work with both regular styling and conditional binding. I've exhausted all the solutions on stackoverflow, but nothing seems to be working for m ...