Installations
npm install react-dom-instance
Developer Guide
Typescript
No
Module System
CommonJS
Node Version
10.14.2
NPM Version
6.4.1
Score
71.5
Supply Chain
100
Quality
75.4
Maintenance
100
Vulnerability
100
License
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (100%)
Developer
arqex
Download Statistics
Total Downloads
536,029
Last Day
190
Last Week
1,335
Last Month
6,078
Last Year
90,558
GitHub Statistics
12 Stars
20 Commits
1 Forks
3 Watching
15 Branches
1 Contributors
Package Meta Information
Latest Version
2.0.0
Package Id
react-dom-instance@2.0.0
Unpacked Size
9.07 kB
Size
3.62 kB
File Count
5
NPM Version
6.4.1
Node Version
10.14.2
Total Downloads
Cumulative downloads
Total Downloads
536,029
Last day
-36.9%
190
Compared to previous day
Last week
-12.9%
1,335
Compared to previous week
Last month
-2.8%
6,078
Compared to previous month
Last year
84.4%
90,558
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
react-dom-instance
When using react-dom
's findDOMNode()
we get the HTML node generated by a react component instance.
react-dom-instance
's findInstance()
goes in the opposite direction: from an HTML node, we get the react component instance that generated it.
** Only React v16 and higher is supported**
How it works
If we have some component like the following one:
1import * as React from 'react'; 2 3class TestComponent extends React.Component { 4 render(){ 5 return <div id="testNode" data-testid="testNode" /> 6 } 7 sayHello(){ 8 console.log('Hello!'); 9 } 10}
We can call the sayHello
method getting the instance with react-dom-instance
:
1import { findInstance } from 'react-dom-instance'; 2 3const node = document.getElementById('testNode'); 4findInstance( node ).sayHello(); // 'Hello!' in the console
We will only find instances from react's class components, functional components has no instances and
Installation
npm install react-dom-instance
API
findInstance( domNode, {options} )
Given the root domNode
generated by a component, it returns the instance of the component. If can't find any instances linked to the domNode
it returns false
.
Functional components don't have instances, so using findInstance
with one DOM node generated by them will also return false
.
We can pass some options in order to retrieve the wanted instance from the DOM node:
componentName
Sometimes there are more than one component instance linked to a DOM node. For example, when a component render
method returns another component, or when using higher order components.
In those cases we can use componentName
to tell findInstace
what type of component instance we want:
1// These 2 components will be linked to the same DOM node 2class ParentComponet extends React.Component { 3 render() { return <ChildComponent /> } 4} 5 6class ChildComponent extends React.Component { 7 render() { return <div id="sharedNode" /> } 8} 9 10let domNode = document.getElementById('sharedNode'); 11 12// Get the instance of the `ParentComponent` 13findInstance( domNode, {componentName: 'ParentComponent'} ); 14 15// By default, `findInstance` returns the instance of the leaf component 16findInstance( domNode ); // ChildComponent instance
maxIteration
When there are more than one component linked to the same DOM node, findInstance
navigates recursively through the react fibers looking for the instance of the right component we want. To make this process fast and avoid infinite loops there is a maximum number of iterations that the algorithm does until it decides that the instance can't be found. By default findInstance
iterates 4
times looking for the instance and return false
after not finding it.
If you have a DOM node with a lot of instances linked and know that more iterations are needed to find the one you want, you can pass maxIteration
as an option with a bigger number:
1findInstance( domNode, {componentName: 'MyComponent', maxIterations: 6} );
Usage with react testing library
This library was thought as a helper for testing with react testing library: sometimes it is useful to access to the component instances in order to create tests.
To make the integration easier, findInstance
accepts the container returned by react testing library and know how to extract the instance linked to it:
1import { render } from '@testing-library/react'; 2import { findInstance } from 'react-dom-instance'; 3 4it('should say hello', () => { 5 // TestComponent from the first example in this readme 6 const { container } = render( <TestComponent /> ); 7 8 findInstance( container ).sayHello(); 9});
We can also use the query methods to get instances from components down in the react tree:
1it('should say hello', () => { 2 // App component render TestComponent at some point 3 const { queryByTestId } = render( <App /> ); 4 5 findInstance( queryByTestId('testNode') ).sayHello(); 6});
Here's the changelog MIT Licensed
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
Found 0/19 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 2 are checked with a SAST tool
Reason
30 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-v88g-cgmw-v5xw
- Warn: Project is vulnerable to: GHSA-93q8-gq69-wqmw
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-w8qv-6jwh-64r5
- Warn: Project is vulnerable to: GHSA-xp63-6vf5-xf3v
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-gxpj-cx7g-858c
- Warn: Project is vulnerable to: GHSA-w573-4hg7-7wgq
- Warn: Project is vulnerable to: GHSA-896r-f27r-55mw
- Warn: Project is vulnerable to: GHSA-9c47-m6qq-7p4h
- Warn: Project is vulnerable to: GHSA-p6mc-m468-83gw
- Warn: Project is vulnerable to: GHSA-29mw-wpgm-hmr9
- Warn: Project is vulnerable to: GHSA-35jh-r3h4-6jhm
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-xvch-5gv4-984h
- Warn: Project is vulnerable to: GHSA-w7rc-rwvf-8q5r
- Warn: Project is vulnerable to: GHSA-r683-j2x4-v87g
- Warn: Project is vulnerable to: GHSA-5fw9-fq32-wv5p
- Warn: Project is vulnerable to: GHSA-hj48-42vr-x3v9
- Warn: Project is vulnerable to: GHSA-hrpp-h998-j3pp
- Warn: Project is vulnerable to: GHSA-p8p7-x288-28g6
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-jgrx-mgxx-jf9v
- Warn: Project is vulnerable to: GHSA-72xf-g2v4-qvf3
- Warn: Project is vulnerable to: GHSA-j8xg-fqg3-53r7
- Warn: Project is vulnerable to: GHSA-6fc8-4gx4-v693
- Warn: Project is vulnerable to: GHSA-3h5v-q93c-6h6q
- Warn: Project is vulnerable to: GHSA-c4w7-xm78-47vh
Score
1.7
/10
Last Scanned on 2024-12-23
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More