Produce unique ID values for react components
a function that returns the ID of either a Component instance or a properties object:
- if
props.id
exists, resolved toprops.id
- else if this component was already assigned an ID by rule 3, resolved to pre-assigned ID.
- else produce new unique ID (using global counter), store it in the component and resolve to it.
- if
id
exists on argument, resolved toid
- else throw an error
import { globalId } from "wix-react-tools";
class MyComp extends React.Component{
...
render () {
<div id={globalId.getRootId(this)} >
...
</ div>
}
}
or:
function MyComp (p: globalId.Props){
return <div id={globalId.getRootId(p)} >
...
</ div>
}
produce a new globally unique id for the child element (that is not the root of the render function), given an ID of the root element (assumed to be globally unique), and a locally unique id of a child element.
import { globalId } from "wix-react-tools";
globalId.getLocalId(getRootId(p), 'title')
```tsx
function MyComp (p: globalId.Props){
return <div id={globalId.getRootId(p)} >
<div id={globalId.getLocalId(getRootId(p), 'inner_child')}>
</ div>
}