React Element Selector Query (resq) - Query React components and children by selector (component name)
npm install resq- React v16 or higher
- Node 8 or higher
- React DevTools (optional)
This library tries to implement something similar to querySelector and querySelectorAll, but through the React VirtualDOM. You can query for React composite elements or HTML elements. It provides two functions resq$ and resq$$ for selecting one or multiple components, respectively.
``
$ npm install --save resq
$ yarn add resq
`
To get the most out of the library, we recommend you use React Dev Tools to verify the component names you want to select. Granted for basic usage you don't need this as long as you know the component name beforehand, but for Styled components and MaterialUI components it will be of great help.
#### Type definition
`typescript
interface RESQNode {
name: 'MyComponent',
node: HTMLElement | null,
isFragment: boolean,
state: string | boolean | any[] | {},
props: {},
children: RESQNode[]
}
resq$(selector: string, element?: HTMLElement): RESQNode
resq$$(selector: string, element?: HTMLElement): Array
`
* Basic Usage
* Wildcard selection
* Async selection
* Filtering selection
#### Basic Usage
Take this React App:
`jsx
// imports
const MyComponent = () => (
const App = () => (
ReactDOM.render(
`
Selecting MyComponent:
`js
import { resq$ } from 'resq'
const root = document.getElementById('root');
resq$('MyComponent', root);
/*
{
name: 'MyComponent',
node:
#### Wildcard selection
You can select your components by partial name use a wildcard selectors:
`jsx
// importsconst MyComponent = () => (
My Component
)const MyAnotherComponent = () => (
My Another Component
)const App = () => (
)ReactDOM.render( , document.getElementById('root'))
`Selecting both components by wildcard:
`js
import { resq$$ } from 'resq'const root = document.getElementById('root');
resq$$('My*', root);
/*
[
{
name: 'MyComponent',
node:
,
isFragment: false,
state: {},
props: {},
children: []
},
{
name: 'MyAnotherComponent',
node: ,
isFragment: false,
state: {},
props: {},
children: []
},
]
*/
`Selecting
MyAnotherComponent by wildcard:
`js
import { resq$ } from 'resq'const root = document.getElementById('root');
resq$('My*Component', root);
/*
{
name: 'MyAnotherComponent',
node:
,
isFragment: false,
state: {},
props: {},
children: []
}
*/
`#### Async selection
Going by the same example as in basic usage, if you don't want to pass the root element to the function, you can do it this way:
`js
import { resq$, waitToLoadReact } from 'resq'async function getReactElement(name) {
try {
await waitToLoadReact(2000) // time in MS to wait before erroring
return resq$(name)
} catch (error) {
console.warn('resq error', error)
}
}
getReactElement('MyComponent')
`
#### Filtering selection
You can filter your selections
byState or byProps. These are methods attached to the RESQNode return objects.Example app:
`jsx
// importsconst MyComponent = ({ someBooleanProp }) => (
My Component {someBooleanProp ? 'show this' : ''}
)const App = () => (
)ReactDOM.render( , document.getElementById('root'))
`To select the first instance of
MyComponent where someBooleanProp is true:`js
import { resq$ } from 'resq'const root = document.getElementById('root')
const myComponent = resq$('MyComponent', root)
const filtered = myComponent.byProps({ someBooleanProp: true })
console.log(filtered)
/*
{
name: 'MyComponent',
node:
,
isFragment: false,
state: {},
props: {
someBooleanProp: true,
},
children: []
}
*/`Deep Matching with
exact flagIf you are in need of filtering
byProps or byState and require the filter to match exactly every property and value in the object (or nested objects), you can pass the exact flag to the function:`js
import { resq$ } from 'resq'const root = document.getElementById('root')
const myComponent = resq$('MyComponent', root)
const filtered = myComponent.byProps({ someBooleanProp: true }, { exact: true })
``