resq
REact Selector Query (resq) - Query React components and children by selector (component name)
Last updated 6 years ago by baruchvlz .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ gnpm install resq 
SYNC missed versions from official npm registry.

resq (React Element Selector Query) npm Build Status codecov

Requirements

  • 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.

Installation

$ npm install --save resq

$ yarn add resq

Usage

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


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<RESQNode>

Basic Usage

Take this React App:

// imports

const MyComponent = () => (
    <div>My Component</div>
)

const App = () => (
    <div><MyComponent /></div>
)

ReactDOM.render(<App />, document.getElementById('root'))

Selecting MyComponent:

import { resq$ } from 'resq'

const root = document.getElementById('root');
resq$('MyComponent', root);
/*
{
    name: 'MyComponent',
    node: <div />,
    isFragment: false,
    state: {},
    props: {},
    children: []
}
*/

Wildcard selection

You can select your components by partial name use a wildcard selectors:

// imports

const MyComponent = () => (
    <div>My Component</div>
)

const MyAnotherComponent = () => (
    <div>My Another Component</div>
)

const App = () => (
    <div>
        <MyComponent />
        <MyAnotherComponent />
    </div>
)

ReactDOM.render(<App />, document.getElementById('root'))

Selecting both components by wildcard:

import { resq$$ } from 'resq'

const root = document.getElementById('root');
resq$$('My*', root);
/*
[
    {
        name: 'MyComponent',
        node: <div />,
        isFragment: false,
        state: {},
        props: {},
        children: []
    },
    {
        name: 'MyAnotherComponent',
        node: <div />,
        isFragment: false,
        state: {},
        props: {},
        children: []
    },
]
*/

Selecting MyAnotherComponent by wildcard:

import { resq$ } from 'resq'

const root = document.getElementById('root');
resq$('My*Component', root);
/*
{
    name: 'MyAnotherComponent',
    node: <div />,
    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:

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:

// imports

const MyComponent = ({ someBooleanProp }) => (
    <div>My Component {someBooleanProp ? 'show this' : ''} </div>
)

const App = () => (
    <div>
        <MyComponent />
        <MyComponent someBooleanProp={true} />
    </div>
)

ReactDOM.render(<App />, document.getElementById('root'))

To select the first instance of MyComponent where someBooleanProp is true:

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: <div />,
    isFragment: false,
    state: {},
    props: {
        someBooleanProp: true,
    },
    children: []
}
*/

Deep Matching with exact flag

If 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:

import { resq$ } from 'resq'

const root = document.getElementById('root')
const myComponent = resq$('MyComponent', root)
const filtered = myComponent.byProps({ someBooleanProp: true }, { exact: true })

Current Tags

  • 1.8.0-beta.0                                ...           beta.0 (5 years ago)
  • 1.11.0                                ...           latest (2 years ago)
  • 1.9.1                                ...           next (4 years ago)

40 Versions

  • 1.11.0                                ...           2 years ago
  • 1.10.2                                ...           3 years ago
  • 1.10.1                                ...           3 years ago
  • 1.10.0                                ...           4 years ago
  • 1.9.2                                ...           4 years ago
  • 1.9.1                                ...           4 years ago
  • 1.9.0                                ...           4 years ago
  • 1.8.0                                ...           4 years ago
  • 1.7.1                                ...           5 years ago
  • 1.8.0-beta.0                                ...           5 years ago
  • 1.7.0                                ...           5 years ago
  • 1.6.1                                ...           5 years ago
  • 1.6.0                                ...           5 years ago
  • 1.5.0                                ...           6 years ago
  • 1.5.0-beta.0                                ...           6 years ago
  • 1.4.0                                ...           6 years ago
  • 1.4.0-rc.1                                ...           6 years ago
  • 1.4.0-rc.0                                ...           6 years ago
  • 1.3.2                                ...           6 years ago
  • 1.3.1                                ...           6 years ago
  • 1.3.0                                ...           6 years ago
  • 1.3.0-rc.1                                ...           6 years ago
  • 1.2.1                                ...           6 years ago
  • 1.2.1-rc.1                                ...           6 years ago
  • 1.2.1-rc                                ...           6 years ago
  • 1.2.0                                ...           6 years ago
  • 1.2.0-alpha.2                                ...           6 years ago
  • 1.2.0-alpha.1                                ...           6 years ago
  • 1.1.0                                ...           6 years ago
  • 1.0.0                                ...           6 years ago
  • 1.0.0-rc.1                                ...           6 years ago
  • 0.0.1-alpha.4-test.4                                ...           6 years ago
  • 0.0.1-alpha.4-test.3                                ...           6 years ago
  • 0.0.1-alpha.4-test.2                                ...           6 years ago
  • 0.0.1-alpha.4-test                                ...           6 years ago
  • 0.0.1-alpha.4                                ...           6 years ago
  • 0.0.1-alpha.3                                ...           6 years ago
  • 0.0.1-alpha.2                                ...           6 years ago
  • 0.0.1-alpha.1                                ...           6 years ago
  • 0.0.1-alpha                                ...           6 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (1)
Dev Dependencies (15)
Dependents (1)

Copyright 2013 - present © cnpmjs.org | Home |