hampusborgos / react-html-id Goto Github PK
View Code? Open in Web Editor NEWProvides unique IDs for HTML DOM elements.
License: MIT License
Provides unique IDs for HTML DOM elements.
License: MIT License
I have used this library with React and TypeScript and created this typing definition to not have my compiler complain:
import { Component, ComponentLifecycle } from "react";
export declare function enableUniqueIds(component: Component): void;
export declare function resetUniqueIds(): void;
declare module "react" {
export interface Component<P = {}, S = {}, SS = any> extends ComponentLifecycle<P, S, SS> {
nextUniqueId(): string;
lastUniqueId(): string;
getUniqueId(identifier: string): string;
}
}
It is possible another interface would be a better spot for this, however I have used this with React.PureComponent
and observed the expected results (i.e. an element had id="id-2-1"
).
I used it by manually adding the typing definition file to node_modules
. Here is a class with this in use TransformedImagesElement/src/components/editor/inputs/SwitchInput.tsx. Manually adding it is not ideal, which leads to two possible solutions:
Can this library help with generating unique test ids, so that we can use that to target specific elements in tests ?
Currently, this is how it seems to work and some issues:
It would be nice to be able to do something like:
uniqueId.enableUniqueIds(this, {
prefix: "checkbox"
});
So, that the first instance id of that type would be checkbox-1
, the second instance id of that type would be checkbox-2
, and so on...
Is there a way to use this with stateless functional components?
It looks like this library uses the lifecycle method "componentWillUpdate" which is deprecated in React 16.3 and will be removed in the future.
I'm not sure what we can use as an alternative, maybe switching to the UNSAFE variant for now?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.