Struggles working with css in react applications

Looking for some assistance here...

In the code snippet below, I have two separate elements that I want to display one at a time.

The first element is an iframe and the second is a div.

The idea is that if the user clicks on the {title}, the frame disappears and the div appears.

I've been able to make the frame disappear and appear by clicking on the title, but the same does not happen with the div.

Even though the code is essentially the same, I'm confused as to why the div isn't behaving like the frame.

I've double-checked and confirmed that both CSS classes are being changed as expected; it's just that the CSS class doesn't seem to work on the div.

Thanks in advance for any help!

import React, { useState } from 'react';

const Card = (props) => {
    const { id, title, active, site, img } = props.data;
    const [content, setContent] = useState(false);

    return (
        <div className={`card ${active && 'active'}`} >
            <img id='img_cover' src={img} alt='image01' onClick={() => props.onCardClick(id)}></img>
            <div className='txt'>
                <h2 onClick={() => setContent(!content)}>{title}</h2>
            </div>
            <iframe className={`${content ? 'content_site' : 'content_frame'}`} src={site} frameborder="0" title={title}>
            </iframe>
            <div className={`${content ? 'content_frame' : 'content_site'}`}>
                <form id="contact-form" action="#" className="table">
                    <input className='input_espace row' id='nome' placeholder="Name" name="name" type="text" required />
                    <input className='input_espace row' id='email' placeholder="Email" name="email" type="email" required />
                    <textarea id="text_area" className='row' cols="50" placeholder="Message" type="text" name="message" />
                    <button type="button" class="btn btn-outline-warning button_submit"> Send</button>
                </form>
            </div>
        </div >
    )
}
export default Card;

Answer №1

className={`${content ? 'content_site' : 'content_frame'}`} 

and

className={`${content ? 'content_frame' : 'content_site'}`}

appear to be conflicting because both are relying on the truth value of content. Modify one to use !content instead.

Adjust the second one as demonstrated in your method setContent(!content).

Here is the updated code snippet:


import React, { useState } from 'react';

const Card = (props) => {
    const { id, title, active, site, img } = props.data;
    const [content, setContent] = useState(false);

    return (
        <div className={`card ${active && 'active'} `}>
            <img id='img_cover' src={img} alt='image01' onClick={() => props.onCardClick(id)}></img>
            <div className='txt'>
                <h2 onClick={() => setContent(!content)}>{title}</h2>
            </div>
            <iframe className={`${content ? 'content_site' : 'content_frame'}`} src={site} frameborder="0" title={title}></iframe>
            <div className={`${!content ? 'content_frame' : 'content_site'}`}>
                <form id="contact-form" action="#" className="table">
                    <input className='input_espace row' id='nome' placeholder="Name" name="name" type="text" required />
                    <input className='input_espace row' id='email' placeholder="Email" name="email" type="email" required />
                    <textarea id="text_area" className='row' cols="50" placeholder="Message" type="text" name="message"></textarea>
                    <button type="button" class="btn btn-outline-warning button_submit">Enviar</button>
                </form>
            </div>
        </div>
    )
}
export default Card;


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

Searching for text within an HTML document using Selenium in Python can be easily achieved by using the appropriate methods and

Is there a way to find and retrieve specific texts within an HTML file using Selenium in Python, especially if the text is not enclosed within an element? <div class="datagrid row"> ==$0 <h2 class="bottom-border block">Accepted Shipment</h ...

How can I ensure text remains confined within the parent div when working with Bootstrap?

I'm having trouble keeping the text and links within their parent div. Even though I set the outer boundary as col-sm-8, the text and links are overflowing. I'm not sure what I'm doing wrong, but the code I am using is shown below. I have a ...

Having trouble understanding why <p> and <h3> elements are displaying as links?

Starting my journey in front-end development with my first website project. Using HTML, CSS, Bootstrap, and Flask. Encountering an issue where my code is styled like a link instead of plain text. It appears blue and becomes underlined when hovered over. & ...

Achieving CSS Buttons That Change to Green When Clicked and STAY Green

I've been tasked with working on a front-end project for a website that involves buttons changing color when clicked and staying that color. Here is the CSS <style> code I have written for these specific buttons: .button { background-color ...

Creating a ThreeJS scrollable plane within a React application: A step-by-step guide

I am interested in using ThreeJS to create a 2D plane with clearly defined boundaries on the left, right, top, and bottom edges. I want to ensure that the camera cannot scroll past these limits. The reason for choosing ThreeJS for a scene that is essential ...

Is it possible to showcase dates within input text boxes prior to POSTing the form?

I am currently working on a form that posts to a PHP script by selecting a date range. For a better understanding, you can check out my visual representation here. Before the data is posted, I would like to display the selected dates in empty boxes or inpu ...

What are the best practices for utilizing fetch() to retrieve data from a web API effectively?

Is there a way to store stock data in stockData and display it in the console upon form submission? Upon submitting the form, I only receive undefined. I suspect this may be due to a delay in fetching data from the API (but not certain). How can I resol ...

Steps to insert an h2 element within a MeanMenu container

My current project involves working on a website that utilizes the meanmenu plugin. I have been facing a challenge for the past two days in trying to display a title inside the mean menu container. Here is the HTML code snippet for the menu: ...

How can we avoid a runtime error when attempting to filter an array that may not be present at a certain point in

Encountering a runtime error with my reducer state chunk - specifically a TypeError stating that an intermediate value is not iterable. This occurs when the object, childGroup, may not be present in the state at a given moment. Any solutions on how to avoi ...

Django is refusing to display my CSS and JavaScript files - static files

Within my code in settings.py, I define the static URL like so: STATIC_ROOT = os.path.join(BASE_DIR, "static") STATIC_URL = '/static/' The location of my static files and folders is at /home/myLinuxUsername/myApp/static/interactive-preview-depe ...

A guide to toggling the check/uncheck status of a list box by clicking on a checkbox using

I am using a div and CSS id to control the checkbox check and uncheck functionality, but I am not getting the desired outcome from my source code Step 1: By default, the checkbox is unchecked and the listbox is disabled Step 2: When the checkbox is check ...

Attempting to choose everything with the exception of a singular element using the not() function in Jquery

Within the mosaic (div #mosaique) are various div elements. My goal is to make it so that when I click on a specific div, like #langages or #libraries, the other divs become opaque while the selected div undergoes a size change (which works correctly in t ...

Arranging two stretchable div elements side by side

I'm currently working on a web page layout where I want two divs to be flexible and adjust their size as the browser window is resized. However, I've encountered an issue where instead of shrinking, the line breaks between the two divs when the w ...

Bringing a React Context Back to its Original State

The default state of a React Context is defined as: export const defaultState: UsersState = { isModalOpen: false, isCancelRequest: false, companyId: 0, users: [] }; After cancelling the modal, the goal is to reset the state back to its default va ...

Interact with React, Express, and Fetch to successfully send and receive byte arrays

I am currently on a quest to find a reliable way to transfer base64 data from my Express server to my React app. The Express server reads an image, converts it to base64, then to a byteArray before sending it to the client's react-app. const reques ...

Navigate through a specific div element

I am facing a dilemma with the following code: #points{ width:25%; background:#2a2a2a; height:20px; color:white; z-index:2; position:absolute; } #areas{ position:absolute; color:white; border-right:2px solid black; height:120%; ...

React does not play well with the Sendgrid Node.js library

Seeking assistance with integrating node.js to handle email sending on my website. Interested in having the email form immediately send upon submission, rather than using the standard "mailto" action. Utilizing Sendgrid as the email service for API and ser ...

Is the required attribute for form submission in Material UI not verifying in another file?

It seems that I may be importing incorrectly because a required field does not display a "please fill in this field" popover when attempting to submit a form. The imported classes are as follows: import * as React from 'react' import FormContro ...

Styling an element in CSS3 based on the status of an input checkbox

Can someone explain the purpose of using '~' in this code snippet? I came across this example in a tutorial and am curious about its significance. http://css-tricks.com/the-checkbox-hack/ I understand that it alters the styling of an element bas ...

Passing a data object dynamically through routing in Next.js

https://i.stack.imgur.com/e78s6.png I've encountered an issue while trying to pass an object to another route. My objective is to navigate to the edit page with the data of a specific row when the edit button is clicked. Passing just the id works fin ...