Utilizing distinct useState for mapped elements

I am struggling to find a solution on how to pass specific useState states to respective mapped elements.

export const Polska = () => {
  const [riverVisible, setRiverVisible] = useState(false)
  const [mountainVisible, setMountainVisible] = useState(false)
  const [lakeVisible, setLakeVisible] = useState(false)

  const [currentSlide, setCurrentSlide] = useState(0)

  const handleClick = (direction) => {
    direction === 'left' 
    ? setCurrentSlide(currentSlide > 0 ? currentSlide - 1 : 2) 
    : setCurrentSlide(currentSlide < 3 -1 ? currentSlide + 1 : 0)
  }
 return (
    <div className='polska'>
<Row>
        <div className='listOfLandscapes'>
          <div className="listOfLandscapes__slider" style={{transform: `translateX(-${currentSlide * 100}vw)` }}>
            {/* {
              PolskaLandscapes.map((landscape) => */}
                <div className='listOfLandscapes__slider__item'>
                    <SvgMountains />
                    <button className='listOfLandscapes__slider__item__desc' onClick={() => setMountainVisible(!mountainVisible)}>
                        Breathtaking Mountains
                    </button>
                </div>
                <div className='listOfLandscapes__slider__item'>
                    <SvgRivers />
                    <button className='listOfLandscapes__slider__item__desc' onClick={() => setRiverVisible(!riverVisible)}>
                        The Majestic Rivers
                    </button>
                </div>
                <div className='listOfLandscapes__slider__item'>
                    <SvgLakes />
                    <button className='listOfLandscapes__slider__item__desc' onClick={() => setLakeVisible(!lakeVisible)}>
                        Enchanting Lakes
                    </button>
                </div>
              {/* )
            } */}
          </div>
        </div>
      </Row>

In the code above, everything is hardcoded. However, I believe there must be a way to make it cleaner. I attempted to pass it in the object properties, but it didn't work...

import SvgMountains from "./svgPolska/SvgMountains"
import SvgRivers from "./svgPolska/SvgRivers"
import SvgLakes from "./svgPolska/SvgLakes"

export const PolskaLandscapes = [
    {
        id: 1,
        bcg: <SvgMountains />,
        desc: 'Breathtaking Mountains',
        v: riverVisible,
        sv: setRiverVisible
    },
    {
        id: 2,
        bcg: <SvgRivers />,
        desc: 'The Majestic Rivers',
        v: mountainVisible,
        sv: setMountainVisible
    },
    {
        id: 3,
        bcg: <SvgLakes />,
        desc: 'Enchanting Lakes',
        v: lakeVisible,
        sv: setLakeVisible
    }
]

I would greatly appreciate any insights.

Answer №1

To improve clarity, you can implement a landscape item component for a more organized approach.

const createLandscapeItem = (id, background, description) => {
    const [visibleStatus, setVisibleStatus] = useState(false);

    return ...
}

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

Unable to include query parameters in the nextjs dynamic route

I have a file called [slug].js. What I am attempting to do is add a query parameter to this dynamic route. Here's the code: await router.replace({ pathname: `${router.pathname}`, query: { coupon }, }, undefined, ...

Navigating following a JQuery AJAX request in PHP

After clicking the login button, I utilize JQuery's POST method to retrieve data from login.php to check if the login was successful. If it fails (no user found), the appropriate message is displayed. However, when attempting to redirect a user (whic ...

single-use date availability checker

I'm currently facing an issue with my code. It seems to be functioning correctly, but only for the first iteration. Let me explain further. If I select a date and time that is already in the database, it correctly displays "date unavailable". Likewis ...

Bidirectional communication between two AngularJS scopes or controllers utilizing a service

I am facing numerous situations where I require clicks or other interactions to trigger actions in a different area of the webpage (one-way communication). Now, I have encountered a need for bidirectional communication, where changes made in element A can ...

Activate a particular panel within the leftPanel using PDFNet Webviewer

When using disableElements in the setQuickBarPanelContents() function, I am able to remove 2 of the 3 panels from the leftPanel: setQuickBarPanelContents() { this.instance.disableElements(['notesPanel', 'notesPanelButton', &apos ...

Removing an event from Fullcalendar

With the help of a post on StackOverflow, I managed to modify my select: method to prevent users from adding an event on a date before NOW. However, there is a drawback. When a user clicks on an empty time slot and the system displays an alert message, th ...

Withdrawal of answer from AJAX request

Is there a way to create a function that specifically removes the response from an AJAX call that is added to the inner HTML of an ID? function remove_chat_response(name){ var name = name; $.ajax({ type: 'post', url: 'removechat.php ...

Achiever.js - Incorporating incremental progress with half stars instead of whole stars

Hello there! I've been utilizing Rater.js in my current project, and so far it has provided me with satisfactory results. However, I have encountered a particular issue that I am struggling to resolve on my own. It would be greatly appreciated if you ...

Merge the JSON data with the Node.js/Express.js response

Whenever I input somedomain.com/some_api_url?_var1=1 in a browser, the response that I receive is {"1":"descriptive string"}. In this JSON response, the index 1 can vary from 1 to n, and the "descriptive string" summarizes what the index represents. I am ...

Setting up Datatables using AngularJS

I am working on a controller that organizes song rankings based on sales data. Upon initialization, the controller automatically sends an HTTP GET request to retrieve all the songs needed for display (currently set at the top 20 songs). If I ever need to a ...

Drag and drop functionality in Material UI Grid

Is there a simple way to convert my Material-Ui Grid into a drag and drop grid? I've already attempted using the react-grid-layout package, but it produced unexpected results. Can someone suggest an easy method for making the Material Ui grid draggabl ...

Ensure that the heading cells in the table header (thead) are properly

Struggling with aligning the thead with the td in the tbody? I attempted adjusting the thead using display: table-header-group;, but discovered that padding doesn't work on 'table-header-group'. This led me to try adding padding-left to my t ...

Display one div and conceal all others

Currently, I am implementing a toggle effect using fadeIn and fadeOut methods upon clicking a button. The JavaScript function I have created for this purpose is as follows: function showHide(divId){ if (document.getElementById(divID).style.display == ...

Resizing an image within an anchor tag

Currently, I am working on styling a page in NextJS. The challenge I'm facing is resizing an image that is inside an anchor within a div. Despite numerous attempts, the image refuses to resize as desired. I've experimented with applying properti ...

Display excerpts of code within an Angular application within the HTML segment

I'm currently developing a tutorial page using Angular where I intend to display code snippets in various programming languages such as 'Java', 'TypeScript', 'C#', and 'Html'. Although there is a snippet called ...

What is the reason behind IE's decision to reduce the size of password boxes compared to text

Take a look at the uncomplicated form right below. You'll notice that it consists of a text box positioned above a password box. In older versions of Internet Explorer like 7 and 8, as well as possibly in other browsers, you might observe that the tex ...

How to Invoke JavaScript Functions within jQuery Functions?

I'm working on a jQuery function that involves dynamically loading images and checking their width. I have two JavaScript functions, func1() and func2(), that I want to call from within the jQuery function in order to check the width of each image. T ...

Using getJSON to return key/value pair from local host URL in JSFiddle - A step-by-step guide

After following a tutorial on building an API using Python, Flask, SQLite, and SQLAlchemy, I have successfully tested the connection by hitting the localhost address in my browser. Now, I am curious if it is possible to test this connection and see the des ...

Encountering an undefined json array when making an AJAX request

There have been numerous questions on this topic, but none of the specific solutions seemed to apply to my situation. So, I apologize if this is a duplicate query. I am currently working on fetching data from an SQL database using a PHP file that passes t ...

Unable to utilize routes in Express.JS when integrated with nginx

After setting up nginx in front of Express.JS, everything seemed to be running smoothly. However, I encountered an issue when trying to access website.com/users, which resulted in a 404 Not Found error. It appears that accessing website.com works fine, but ...