3.1.2 • Published 4 years ago

react-sequential-id v3.1.2

Weekly downloads
1
License
MIT
Repository
github
Last release
4 years ago

React Sequential ID

Component which gives a sequential ID to a function child

Installation

npm install react-sequential-id

Usage

import { IdProvider, SequentialId } from 'react-sequential-id'

function ReactComponent() {
  return (
    <IdProvider>
      <SequentialId>
        {(id) => (
          <p>
            <label for={id}>Label: <input id={id} name="input" /></label>
          </p>
        )}
      </SequentialId>
    </IdProvider>
  )
}

IdProvider

Using an IdProvider somewhere near the top of the app hierarchy is highly recommended. This ensures that rerenders (like will often happens on the server) won't give you different IDs as the sequence is started anew for the children of an IdProvider. It also takes an optional factory prop if you want to use a different factory function (for example, using a different prefix):

import uniqueid from 'uniqueid'

import { IdProvider, SequentialId } from 'react-sequential-id'

function ReactComponent() {
  return (
    <IdProvider factory={uniqueid('custom_prefix_')}>
      <SequentialId>
        {(id /* === 'custom_prefix_0' */) => (
          <p>
            <label for={id}>Label: <input id={id} name="input" /></label>
          </p>
        )}
      </SequentialId>
    </IdProvider>
  )
}

Consumer

If you need access to the ID factory currently in use then you can use the Consumer component.

import { Consumer } from 'react-sequential-id'

function ReactComponent() {
  return (
    <Consumer>
      {(idFactory) => {
        const id = idFactory()
        return (
          <p>
            <label for={id}>Label: <input id={id} name="input" /></label>
          </p>
        )
      }}
    </Consumer>
  )
}

createIdFactory

This function creates an ID factory like the one used by the default component. It is a sequentially increasing integer prefixed by the letter i.

import { createIdFactory } from 'react-sequential-id'

const idFactory = createIdFactory()
const id = idFactory() // id === 'i0'

Legal

Copyright © 2017-2019 Reidar Djupvik

Licensed under the MIT license. See the LICENSE file for details.

3.1.2

4 years ago

3.1.1

5 years ago

3.1.0

6 years ago

3.0.3

6 years ago

3.0.2

6 years ago

3.0.1

6 years ago

3.0.0

6 years ago

3.0.0-beta.1

6 years ago

2.0.0

6 years ago

1.0.0

6 years ago

0.1.8

7 years ago

0.1.7

7 years ago

0.1.6

7 years ago

0.1.5

7 years ago

0.1.4

7 years ago

0.1.3

7 years ago

0.1.2

7 years ago

0.1.1

7 years ago

0.1.0

7 years ago