Enhancing JSX Component with Transition Effect in React

Is there a way to incorporate a transition effect when the button is clicked within this React component? Since the code modifies the JSX content instead of the className, traditional CSS transitions may not work. How can I implement a smooth transition effect in this scenario?

import React, { useState } from 'react';
import ChatItem from './ChatItem';
import ChatButton from './assets/ChatButton';
import classes from './ChatBot.module.css';

const ChatList = (props) => {
  const [selected, setSelected] = useState(props.items[0]);

  function handleChangeSelected(item) {
    setSelected(item);
  }

  const questionButtons = props.items.map((item) => {
    if (item.id === selected.id) return null;
    return (
      <ChatButton onClick={handleChangeSelected.bind(null, item)}>
        {item.question}
      </ChatButton>
    );
  });

  return (
    <div className={classes.faq}>
      <section className="textbox">
        <ChatItem
          key={selected.id}
          id={selected.id}
          question={selected.question}
          answer={selected.answer}
        />
      </section>
      <div className={classes.questions}>{questionButtons}</div>
    </div>
  );
};

export default ChatList;

Answer №1

One way to achieve this is by utilizing a library such as react transition group. However, if the animation is not overly complex, applying a className may be a simpler solution. You can then add your transition to the button and consider using visibility along the way.

<ChatButton className={item.id === selected.id && 'active'} onClick={handleChangeSelected.bind(null, item)}>
   {item.question}
</ChatButton>

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

What is the best way to resize an image back to its original dimensions using CSS?

Is there a way to maintain the original size and aspect ratio of an image in CSS? HTML <img src="https://www.gstatic.com/webp/gallery/1.jpg" /> CSS img { width: 320px; height: 320px; position: relative; transition: 0.2s ease; } i ...

Adjusting iframes for responsive design using only CSS

It's common for websites to have embedded Youtube videos, and with Youtube now compatible on phones too. With the rise of responsive design, it only makes sense for iframes containing Youtube videos to also be responsive, right? After spending days s ...

Obtaining file image input in TypeScript

I am struggling to integrate a file uploaded via the input type "file" into my Fabric JS canvas. The steps of the process are as follows: User initiates action by pressing a button (calls onAddImage) User selects an image to upload Selected image is adde ...

Ways to verify the element prior to the completion of the request?

Utilizing Angular and Playwright Within my application, I have incorporated 2 buttons - one for delete mode and another for refreshing. Whenever the user triggers a refresh action, the delete mode button is disabled. Once the request returns, the delete m ...

What is the method to ensure background images are loaded with SSL from a different domain?

I run an online store and to improve performance, the images are hosted on a different domain. However, when I switch to secure mode using SSL on certain parts of the website, the background images disappear. Trying to view the image directly in the browse ...

Creating a dynamic dropdown menu in HTML to showcase a variety of images

I am trying to create a drop down menu where each selection displays multiple elements. For example, if sensor 1 is chosen, I want to show a picture of its location and its address. I am having trouble figuring out how to add these functions to the drop ...

Revamping the Jquery Waypoint

Greetings, I am currently delving into the world of Jquery waypoints and facing a bit of challenge getting it to work. My goal is to have an "alert" pop up once an element becomes visible on the screen, but for some reason, the alert doesn't show up w ...

Displaying CSS image slideshow images side by side

Currently I am a student studying the fundamentals of HTML and CSS. My latest project involved creating a website showcasing my work from digital arts class, complete with a simple CSS slideshow. The original intention was to have each image displayed on i ...

Incorporating a radio button input into the parent component in a React application

The main component stores all the state data. Whenever there is a change in any input field, the function myChangeHandler is triggered and updates the state accordingly. myChangeHandler(event) { this.setState({ [event.target.name]: event.target.value }) ...

Displaying Google Street View and Google Maps with marker points in a parallel arrangement

Is there a way to align the Street View window with marker points from Google Maps? Here is the code that needs attention: <div id="property_map"></div> <script> /* Code for Google Map */ func ...

Errors encountered when using TypeScript with destructured variables and props not being recognized

I have a function that returns data. The object is structured with properties such as headerMenu, page, content, and footer. These properties are defined in DataProps interface. When I try to destructure the data object using the line: const { headerMenu, ...

Secure your website against XSS attacks with DOMPurify

Currently I am working on resolving a cross-site scripting (XSS) vulnerability using DOMPurify. The issue lies in the URL provided below: https://stage-xyzmysite.com/login/?rUrl=javascript:alert('hi'). In order to create a proof of concept, I am ...

Infinite scrolling in AngularJS doesn't seem to be functioning properly when using Chrome in full

Currently, I am implementing an infinite scroll feature using ng-repeat and adjusting the limitTo value through a loadMore() function. Below is the code snippet for the directive (discovered on a jsfiddle): angular.module('scroll', []).directiv ...

Building Your Own Custom Mobile Global Breakpoint Plugin with Vuetify in Nuxt.js

Looking to set up a custom breakpoint system for my Nuxt/Vuetify project so I can easily manage it from one centralized location instead of using $vuetif.breakpoint..... etc To achieve this, I have created a plugin file named mobile.js. While it functions ...

Expanding divs automatically depending on the content they contain

Currently, I have been developing a template for a database-driven page that is supposed to contain four divs for content arranged like this: ----- ----- | 1 | | 2 | ----- ----- ----- ----- | 3 | | 4 | ----- ----- The information in each box will always ...

Is there a way to retrieve the value of a Promise?

When making a call to a Promise API for returning from an AJAX request, I would like to have the return value in the then() function structured as follows: function get_data(url, id) { return new Promise(function (resolve, reject) { xmlHttp.open("GET" ...

Unexpected behavior: JQuery Ajax request not displaying Json object following recent update to JQuery version 1.10.2

Currently facing an issue with a project I am working on. The previous programmer used jquery 1.4.4, and I have updated it to 1.10.2 due to the designer using Bootstrap. However, after running it in version 1.10.2, one of the objects that was functional i ...

What is causing this form to submit?

I need help with sending emails via AJAX. My problem is that the form keeps submitting and refreshing, even though I haven't used GET to send anything in the URL. HTML: <form onsubmit="ajaxEmail(); return false;" > <input type=" ...

Using AngularJS to bind to the 'src' property of an <img> tag

There is a table on my website with numerous rows, and each row contains a preview image that should appear in the top right corner when the mouse hovers over it. I attempted to insert an image tag with AngularJS binding for the URL in the src attribute l ...

Looking for a way to convert 24-hour format to minutes in Angular? I'm in need of some TypeScript code to

I am looking for Typescript code that can convert 24-hour time format into minutes. For example, when converting 1.00 it should output as 60 minutes. When converting 1.30 it should equal to 90 minutes. If anyone has the code for this conversion, p ...