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

275
Views
Incremental counter [hook?] inside of React component

I have a component that is used throughout my application. There is a list of elements in the component, and I need to generate tab indexes for these elements.

However, indices should be divided into groups. For example, first group of indices might start with 1000 offset, another group with 2000, so tab indices are different, like:

<p tabindex="1001">...</p>
<p tabindex="1002">...</p>
<p tabindex="1003">...</p>
<p tabindex="1004">...</p>

<p tabindex="2001">...</p>
<p tabindex="2002">...</p>
<p tabindex="2003">...</p>
<p tabindex="2004">...</p>

What I'm currently doing is something like:

          {attachmentsData?.data?.map((attachment, tabIndex) => (
                <FileName tabIndex={1000 + tabIndex} onClick={...}>
                  {attachment.filename}
                </FileName>
              </div>
              <AttachmentButtonContainer>
                <DownloadIcon
                  tabIndex={2000 + tabIndex}...

I'm looking to create a hook/function, so I can use it this way:

          {attachmentsData?.data?.map((attachment, tabIndex) => (
                <FileName tabIndex={getNext('group1')} onClick={...}>
                  {attachment.filename}
                </FileName>
              </div>
              <AttachmentButtonContainer>
                <DownloadIcon
                  tabIndex={getNext('group2')}...

But not sure what's the idiomatic way to do that. Basically, the idea is that getNext should generate indices regardless of how many times this component gets rendered on the page: one time o hundred times. So we should keep getNext as a parameter to the component, right?

How exactly it can look like, so once you call it, the counter gets increased? I tried to create my own hook based on useState, however it has two functions - one to read the value, one to set the value. I want to combine things - once you read the value, it gets increased, started from some offset. The offset can be random, 1000 or 10000, doesn't matter too much. The only thing is that it should be sequential, so users can use keyboard to navigate.

Maybe you can give me some other advice on how to architecture this. Thanks!

about 4 years ago · Juan Pablo Isaza
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!