Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

87
Views
How to distinguish user clicks between different buttons?

I have rendered 3 plan options from an object array I get from the backend.

If the plan being rendered is

  • cheaper, the user's subscription then its corresponding button would say downgrade,
  • costlier, the button would say upgrade, if it is the same then the button would say current.

This logic for rendering is working fine.

But when users click on a button I am not able to identify which option they clicked. I need to update the handle click events based on the plan being rendered.

Currently, I am iterating through the tiers map, and for each tier, I am rendering the button with appropriate text. The function to display the text basically checks for the tiered pricing and returns upgrade or downgrade or current plan. I need a way to update the handle click just like the text.

How can I dynamically update the handle click events based on the tier being rendered?

const [confirmation, setConfirmation] = useState(false);
const handleConfirmation = () => {
    setConfirmation(true);
    console.log('button is clicked');
};

<div className='px-8 lg:px-24 grid grid-cols-3 gap-8'>
    {tiers.map((tier) => (
        <div
            key={tier.title}
            className='relative p-8 bg-white border border-gray-200 rounded-2xl shadow-sm flex flex-col'
        >
            <div className='flex-1'>
                <h3 className='text-xl font-semibold text-gray-900'>{tier.title}</h3>
                {tier.value ? (
                    <p className='absolute top-0 py-1.5 px-4 bg-grays-600 rounded-full text-xs font-semibold uppercase tracking-wide text-white transform-translate-y-1/2'>
                        VALUE PLAN
                    </p>
                ) : tier.recommended ? (
                    <p className='absolute top-0 py-1.5 px-4 bg-blue-500 rounded-full text-xs font-semibold uppercase tracking-wide text-white transform -translate-y-1/2'>
                        RECOMMENDED PLAN
                    </p>
                ) : (
                    ''
                )}
                <Button
                    text={determineCTAText(tier)}
                    size='fullwidth'
                    variant='primary'
                    className='m-auto mt-8 text-center block'
                    handleClick={handleConfirmation}> 
                </Button>
    }

                   
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can modify the handleConfirmation function to receive a tier object and return a function instead. This will create a closure over the tier value and let you access it when the button's callback is called.

const handleConfirmation = (tier) => {
    // Returns the callback function that will be called, with the specific `tier` value in scope
    return () => {
        setConfirmation(true);
        // You can access any `tier` property here
        console.log(`button for tier ${tier.title} was clicked`);
    };
}

return (
    <div className='px-8 lg:px-24 grid grid-cols-3 gap-8'>
        {tiers.map((tier) => (
            {/* Omitted rest of the JSX for simplicity */}
            <Button
                text={determineCTAText(tier)}
                size='fullwidth'
                variant='primary'
                className='m-auto mt-8 text-centerblock'
                handleClick={handleConfirmation(tier)}> 
            </Button>
        }
    </div>
)
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!