Utilizing the map function in React to create a button reference

I am currently facing an issue that is relatively simplistic and doesn't have any real-world application.

My goal is to locate a 'green' button and make it blink for a duration of 3 seconds. How can I achieve this using React?

const btnLayout = ["Yellow", "Green", "Blue", "Orange"];

btnLayout.map((item, index) => {
  return (
    <button>
      data-id={`slot-${item}`}
      type="button" className={`col-1 btn btn-${colorMap[index]}`}
      {item}
    </button>
  );
});

Answer №1

Implement a blinking effect using CSS animations and control it with animation-iteration-count.

const btnLayout = ["Yellow", "Green", "Blue", "Orange"];

btnLayout.map((item, index) => {
  return (
    <button>
      data-id={`slot-${item}`}
      type="button" className={`col-1 btn btn-${colorMap[index]} ${item === "Green" ? "blink_me" : ""}`}
      {item}
    </button>
  );
});

CSS classes

.blink_me {
  animation: blinker 1s linear infinite;
  animation-iteration-count: 3;
}

@keyframes blinker {
  50% {
    opacity: 0;
  }
}

View the code in CodeSandbox

Answer №2

To retrieve the button node in the DOM, utilize ref functionality. For a demonstration, visit this codesandbox.

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

having trouble with starting the React script

Working on a React project in the Intellij IDE, I encountered an issue when attempting to start it with npm. The error message pointed to an issue with the start script in the package.json. I've followed common advice such as reinstalling node.js and ...

Find distinct elements in an array of objects

Imagine you have an array filled with different objects: var itemsArray = [ {name: "apple", color: "red", weight: "100g"}, {name: "banana", color: "yellow", weight: "120g"}, {name: "apple", color: "red", weight: "100g"}, {name: "banana", color: "y ...

The header and navigation bar are both set to stay in place, but unfortunately my div element is not displaying beneath

After setting both the Nav and Header to fixed, everything seems to start out well. I adjust the Nav margin-top so that it appears below the header, which works fine. However, things take a wrong turn when I try adjusting the div. The div ends up behind th ...

Drop effect added to useGesture drag functionality

In my project, I am utilizing react-use-gesture to enable dragging a div horizontally along the x-axis. The implementation is straightforward - it saves the movement value 'x' and utilizes it to adjust the 'left' CSS property of the div ...

"The React Navigation header is not behaving as expected in hiding or showing itself

I'm currently utilizing react navigation and I have a requirement to toggle the header visibility based on button onPress events. However, despite being able to hide the header successfully, I am facing challenges in showing it again using another fun ...

AngularJS making use of multiple checkboxes to filter data from a nested JSON structure

Hi everyone, I came across a helpful resource on implementing multiple checkboxes filters in AngularJS: angular js multiple checkbox with custom filters I am facing a similar issue but with a multilevel JSON structure. Here is an example of what I am deal ...

Struggling with React Native and WebSocket integration issues

I've heard some concerns about Socket.io not functioning properly in React Native, so I opted to utilize plain WebSocket instead. Setting up a WebSocket server with node.js was relatively straightforward for me. While all my attempts were successful ...

Managing extensive amounts of data with server-side scripting in a Datatable

I am exploring the use of the datatable plugin to effectively manage a large amount of data. Currently, I am interested in implementing "server side processing in datatables" with the help of server-side scripting. Since I have limited experience with AJA ...

In the event that the final calculated total is a negative number, reset it to zero. Inform the user of an error through the use of a prompt dialog box

I'm having trouble getting the grand total to display as 0 when I enter all amounts in positive values and then change the unit prices to negative values. However, it works fine when I only enter negative values throughout. Can someone please help me ...

The interface 'IProduct' does not include several properties found in type 'IProduct[]', such as length, pop, push, concat, and many more

My goal is to transfer data between parent and child components using React and TypeScript. I have defined the following interfaces: export interface IProduct { id: string; name: string; price: string; image: string; ...

Decrease the border-radius shorthand mixin, deactivate the variable

I have been working on creating a mixin for border-radius that will only apply when the values, determined by a variable, are greater than or equal to 0. I have set the default value in a variable as 3px, so if I input -1 or 'no', the border-radi ...

Activate automatic click or force trigger JavaScript function

In the MVC view I am working with, there is a form that allows for file submission. //Submit file <% using (Html.BeginForm("MethodName","ControllerName", FormMethod.Post, new { enctype = "multipart/form-data"})) { %> <input type=" ...

The onCreated listener function in the jBox Modal dialog is experiencing issues and not functioning properly after the first use

I am using jBox from the URL: . Every time I click on a link, a modal is created. The first time the modal is created, listeners for the buttons on that modal are properly added in the "onCreated" attribute and work when clicked. However, from the second ...

The grid items fail to refresh even after I have made changes to the useState in my React Native application

I am currently working on a react native project that includes a grid component. My goal is to update an array in the state called selectedHomeTypes whenever a user clicks on an item within the grid. Initially, the array is set to contain only one element: ...

By utilizing a function provided within the context, React state will consistently have its original value

After passing functions from one component to its parent and then through context, updating the state works fine. However, there is an issue with accessing the data inside these functions. They continue to show as the initial data rather than the updated v ...

Is the rotate() method in SVG supported on web browsers when passing in additional parameters like angle, centerX, and centerY similar to the CSS rotate

I have recently learned about a rotate transform that allows for rotating around a specified center point using the `rotate(angle, centerX, centerY)` method. However, I have noticed that this method does not seem to work when applied via CSS. Interestingl ...

What are the steps to installing a .deb file offline using Docker?

I am planning to install a .deb file on my Docker container. In my Dockerfile, I execute the following command: RUN apt-get install -y ./fonts/ttf-mscorefonts-installer_3.6_all.deb ROOT Folder |->Dockerfile |->fonts |-> ttf ...

The component's div does not respond to the max-width attribute

I'm currently facing an issue with my chart component in a view. I've been trying to reduce its size using max-width property but it doesn't seem to be working. Below are the relevant code snippets: <div id="chart"> < ...

Is it possible for three.js to integrate information from REST APIs?

Currently, I am in the process of learning three.js and have successfully created basic 3D models using hardcoded values. My goal now is to retrieve these values from a database that I have set up in MSSQL Server. These x, y, and z parameters are stored ...

Generate a dynamic chart using Angular-chart.js on a canvas that is created on the fly

I am facing an issue with displaying a chart or table in a div based on an XHR response. In the case of a chart, I want to replace the div contents with a canvas element that will be used by chart.js to show a graph. When I directly include the canvas ele ...