Using ReactJS to toggle a div upon clicking

I am facing an issue with toggling a div by clicking on text within a toggle bar. I am in the process of recreating a WordPress website template in React JS that was originally built using bootstrap. You can view the original template here.

So far, I have successfully recreated all sections using simple HTML and CSS and rendered them using React's class method (although I plan to switch to function as there is no state involved). However, I have reached a section (the "get directions to event hall" section, second last) where there is a navigation bar that animates, switches the displayed data, and changes the active heading along with a small pointer beneath it. The image below illustrates this:

https://i.sstatic.net/L8Dn4.jpg

By default, the "Venue" heading is active. Clicking on either "Time" or "How to get there" should update the pink pointer position and change its size accordingly. Furthermore, clicking on any of these headings should animate the switch between the content underneath (even though currently, they show the same text).

To achieve this functionality while retaining the current active heading, here is the relevant code snippet:

import React,{Component} from 'react'

class Directions extends Component{
    render(){
        return(
            <section id = "Directions-section">
                <div id = "directions-div">
                    <div id="directions-text">
                        ...
                    </div>
              </section>
         )
        }

This code represents one instance of the div structure. I intend to duplicate this for each heading and apply the recommended approach.

The corresponding CSS:

#Directions-section
{
    ...
}

#directions-div
{
    ...
}

...

P.S: Bootstrap was omitted initially due to a misunderstanding, and integrating it later caused disruptions after months of effort!


Edit: Additionally, I require text changes and animations upon clicking different headings.

Answer №1

Make a simple change to your ul element

<ul>
            {["Location","Date","Directions"].map((item,index) => 
            <li onClick={() => this.setState({chosen: index})} 
          className={this.state.chosen === index ?"selected":null}>{item}</li>)}
         </ul>

In addition,

insert the following code above the render() method

state = { 
        chosen: 0
     }

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

I'm having trouble receiving a response after uploading an image on Cloudinary using React js

Once the image is uploaded using the API, it should return a response. However, I am not receiving any response through the API even after uploading the image. if (pic.type === "image/jpeg" || pic.type === "image/png") { const da ...

Enhance your user experience by implementing a jQuery solution that makes a div

Looking to add a small div on the right side of your screen that moves along with your vertical window slider? Wondering if jQuery has a function to get the current vertical slider position? I thought scrollTop() might work, but it only returns from a spe ...

Disabling directional focus navigation in CSS: A comprehensive guide

Is there a way to disable directional focus navigation properties, such as 'nav-up', 'nav-right', 'nav-down', 'nav-left', for elements on an HTML page? button { position:absolute } button#b1 { top:0; left:50%; ...

Retrieve an element generated by a React script without using the ref attribute

There is a chat button on my website generated by a script (zendesk chat) that creates an iframe with the ID "launcher". I am using Nextjs and I am trying to access this element, but I am unable to attach a ref since I do not have the code. I have been sea ...

Choose a specific location on a vehicle illustration within a pop-up window. The image should be partitioned into 6 separate sections

I have a project for my client where they need to choose a car and then indicate where the damage is located on an image, which is divided into 6 sections. I'm struggling to figure out how to achieve this. I initially thought z-index would work, but ...

What is the best method to achieve a width of 100% for an element (textarea) that has unspecified borders and padding, while also including the borders and padding

Native borders for input controls in various browsers often look more visually appealing compared to those set with CSS. However, the thickness of these native borders and padding can vary across different browsers, making it difficult to predict beforehan ...

Tips for sending parameters in Next.js without server-side rendering

I followed the documentation and tried to pass params as instructed here: https://nextjs.org/docs/routing/dynamic-routes However, I encountered a strange issue where the received params are not in string format. How is it possible for them to be in an arr ...

JavaScript array images are not showing when using the img tag

For this module, I am working on creating a flipbook (magazine) using images stored in a JavaScript array. However, I am facing an issue where the images are not loading up properly. Instead of displaying the image, it shows as [object" htmlimageelement]=" ...

Unforeseen behavior in Ajax success callback

My unordered list contains a href link. Initially, only the welcome page is visible on page load. Upon clicking the list for the first time after page load, the desired results are displayed in the corresponding div as expected. The issue arises when swi ...

Apologies, I encountered an error message saying "npm i react-push

An error occurred while trying to install react-push-notification: Error code ERESOLVE: Unable to resolve dependency tree While resolving dependencies, the following issues were found: react@^17.0.2 from the root project Could not resolve peer depen ...

Tips for including a close button in a slidedown panel

Having this as a functional slidedown effect: ::-webkit-scrollbar { width: 0px; /* remove scrollbar space */ background: transparent; } /* optional: show position indicator in red */ ::-webkit-scrollbar ...

Tips for ensuring a scrollbar remains at the bottom position

I'm facing an issue with a scroll-bar inside a div element. Initially, the position of the scroll-bar is at the top. However, whenever I add text to the div element, the scroll-bar remains in its initial position and does not automatically move to the ...

Leveraging AJAX for managing multiple selection options without the need for a submit button

Currently, I have a page featuring two select buttons. Whenever both are selected, the page should update automatically. Furthermore, each time a new option is chosen, the page needs to keep updating on its own. Below is my HTML code: <form action="" m ...

What is the process of utilizing a <li> for a hover selector in jquery?

I've encountered an issue with a list that I am trying to modify its content when hovering over them. Despite using the id as a selector for triggering the hover function, all list items change color instead of just the intended one. The challenge lie ...

Prevent the onClick event from being triggered by utilizing JSON data

I am trying to implement a feature where a button click is disabled based on a parameter fetched from a JSON file: JSON Parameter "reactJson": { "disable:"true" } Currently, my onClick method is functioning correctly. However, ...

I'm continuously receiving a 404 error when using axios, even after setting up a router with Express

I keep encountering a 404 error when using axios, even though I have created a router. Below is my frontend code with axios: import React, { useEffect, useState } from 'react'; import axios from 'axios'; function RecruitStatus() { ...

Expanding content will not cause Flexbox to stretch

I am facing an issue with a container that includes a header tag and a div. The height of the div is set to 100vh, causing it to consume all available space. Within the div, there are two additional divs. Everything works as expected until I add more conte ...

React Material UI version 5 checkbox does not update value within a function

When the Checkbox is placed under a FormControlLabel and function, the value doesn't change when useState changes another variable. If I comment out the useState, the checkbox works fine. If I move the same code (checkbox and formcontrolLabel) to a ...

Material-UI: The call stack has exceeded the maximum range, causing an Uncaught RangeError

I am currently utilizing the Dialog and Select components offered by Material-UI in conjunction with React. Here is a quick example: import React from 'react'; import { Dialog, MenuItem, Select } from '@material-ui/core'; class Examp ...

I'm having trouble with my React Router Dom Redirect path. Everything seems to be set up correctly, but for some reason, it's not directing my home page

I have been working on adding a private route to my main dashboard, which is my home page, to make it accessible only to authenticated users. I created a new component called PrivateRoute and set up the redirection to the login page if the user is not auth ...