{"id":13565514,"url":"https://github.com/baruchvlz/resq","last_synced_at":"2025-04-03T22:31:37.034Z","repository":{"id":34305434,"uuid":"176531427","full_name":"baruchvlz/resq","owner":"baruchvlz","description":"React Element Selector Query (RESQ) - Query React components and children by component name or HTML selector","archived":false,"fork":false,"pushed_at":"2023-09-12T07:32:15.000Z","size":442,"stargazers_count":178,"open_issues_count":5,"forks_count":28,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-03-18T17:51:55.173Z","etag":null,"topics":["query","react","selector"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/baruchvlz.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-03-19T14:36:27.000Z","updated_at":"2025-01-06T08:35:49.000Z","dependencies_parsed_at":"2024-06-18T12:38:36.978Z","dependency_job_id":"6eea12b8-6359-4515-9937-c228e56240a8","html_url":"https://github.com/baruchvlz/resq","commit_stats":{"total_commits":149,"total_committers":12,"mean_commits":"12.416666666666666","dds":0.4093959731543624,"last_synced_commit":"4e7b61e0f3374dbe11edf8bda518947a7cf11f22"},"previous_names":[],"tags_count":25,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/baruchvlz%2Fresq","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/baruchvlz%2Fresq/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/baruchvlz%2Fresq/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/baruchvlz%2Fresq/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/baruchvlz","download_url":"https://codeload.github.com/baruchvlz/resq/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245579619,"owners_count":20638678,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["query","react","selector"],"created_at":"2024-08-01T13:01:48.832Z","updated_at":"2025-04-03T22:31:36.700Z","avatar_url":"https://github.com/baruchvlz.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"# resq (React Element Selector Query) ![npm](https://img.shields.io/npm/v/resq.svg) [![Build Status](https://travis-ci.org/baruchvlz/resq.svg?branch=master)](https://travis-ci.org/baruchvlz/resq) [![codecov](https://codecov.io/gh/baruchvlz/resq/branch/master/graph/badge.svg)](https://codecov.io/gh/baruchvlz/resq)\n\n## Requirements\n\n- React v16 or higher\n- Node 8 or higher\n- React DevTools (optional)\n\nThis 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.\n\n## Installation\n\n```\n$ npm install --save resq\n\n$ yarn add resq\n```\n\n## Usage\n\nTo 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.\n\n\n#### Type definition\n```typescript\n\ninterface RESQNode {\n    name: string,\n    node: HTMLElement | null,\n    isFragment: boolean,\n    state: string | boolean | any[] | {},\n    props: {},\n    children: RESQNode[]\n}\n\nresq$(selector: string, element?: HTMLElement): RESQNode\nresq$$(selector: string, element?: HTMLElement): Array\u003cRESQNode\u003e\n\n```\n\n* [Basic Usage](README.md#basic-usage)\n* [Wildcard selection](README.md#wildcard-selection)\n* [Async selection](README.md#async-selection)\n* [Filtering selection](README.md#filtering-selection)\n\n#### Basic Usage\nTake this React App:\n\n```jsx\n// imports\n\nconst MyComponent = () =\u003e (\n    \u003cdiv\u003eMy Component\u003c/div\u003e\n)\n\nconst App = () =\u003e (\n    \u003cdiv\u003e\u003cMyComponent /\u003e\u003c/div\u003e\n)\n\nReactDOM.render(\u003cApp /\u003e, document.getElementById('root'))\n```\n\nSelecting `MyComponent`:\n\n```js\nimport { resq$ } from 'resq'\n\nconst root = document.getElementById('root');\nresq$('MyComponent', root);\n/*\n{\n    name: 'MyComponent',\n    node: \u003cdiv /\u003e,\n    isFragment: false,\n    state: {},\n    props: {},\n    children: []\n}\n*/\n```\n\n#### Wildcard selection\n\nYou can select your components by partial name use a wildcard selectors:\n\n```jsx\n// imports\n\nconst MyComponent = () =\u003e (\n    \u003cdiv\u003eMy Component\u003c/div\u003e\n)\n\nconst MyAnotherComponent = () =\u003e (\n    \u003cdiv\u003eMy Another Component\u003c/div\u003e\n)\n\nconst App = () =\u003e (\n    \u003cdiv\u003e\n        \u003cMyComponent /\u003e\n        \u003cMyAnotherComponent /\u003e\n    \u003c/div\u003e\n)\n\nReactDOM.render(\u003cApp /\u003e, document.getElementById('root'))\n```\n\nSelecting both components by wildcard:\n```js\nimport { resq$$ } from 'resq'\n\nconst root = document.getElementById('root');\nresq$$('My*', root);\n/*\n[\n    {\n        name: 'MyComponent',\n        node: \u003cdiv /\u003e,\n        isFragment: false,\n        state: {},\n        props: {},\n        children: []\n    },\n    {\n        name: 'MyAnotherComponent',\n        node: \u003cdiv /\u003e,\n        isFragment: false,\n        state: {},\n        props: {},\n        children: []\n    },\n]\n*/\n```\n\nSelecting `MyAnotherComponent` by wildcard:\n```js\nimport { resq$ } from 'resq'\n\nconst root = document.getElementById('root');\nresq$('My*Component', root);\n/*\n{\n    name: 'MyAnotherComponent',\n    node: \u003cdiv /\u003e,\n    isFragment: false,\n    state: {},\n    props: {},\n    children: []\n}\n*/\n```\n\n#### Async selection\n\nGoing by the same example as in [basic usage](README.md#basic-usage), if you don't want to pass the root element to the function, you can do it this way:\n\n```js\nimport { resq$, waitToLoadReact } from 'resq'\n\nasync function getReactElement(name) {\n    try {\n        await waitToLoadReact(2000) // time in MS to wait before erroring\n\n        return resq$(name)\n    } catch (error) {\n        console.warn('resq error', error)\n    }\n}\n\ngetReactElement('MyComponent')\n```\n\n\n#### Filtering selection\n\nYou can filter your selections `byState` or `byProps`. These are methods attached to the RESQNode return objects.\n\nExample app:\n```jsx\n// imports\n\nconst MyComponent = ({ someBooleanProp }) =\u003e (\n    \u003cdiv\u003eMy Component {someBooleanProp ? 'show this' : ''} \u003c/div\u003e\n)\n\nconst App = () =\u003e (\n    \u003cdiv\u003e\n        \u003cMyComponent /\u003e\n        \u003cMyComponent someBooleanProp={true} /\u003e\n    \u003c/div\u003e\n)\n\nReactDOM.render(\u003cApp /\u003e, document.getElementById('root'))\n```\n\nTo select the first instance of `MyComponent` where `someBooleanProp` is true:\n\n```js\nimport { resq$ } from 'resq'\n\nconst root = document.getElementById('root')\nconst myComponent = resq$('MyComponent', root)\nconst filtered = myComponent.byProps({ someBooleanProp: true })\n\nconsole.log(filtered)\n/*\n{\n    name: 'MyComponent',\n    node: \u003cdiv /\u003e,\n    isFragment: false,\n    state: {},\n    props: {\n        someBooleanProp: true,\n    },\n    children: []\n}\n*/\n\n```\n\n**Deep Matching with `exact` flag**\n\nIf 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:\n\n```js\nimport { resq$ } from 'resq'\n\nconst root = document.getElementById('root')\nconst myComponent = resq$('MyComponent', root)\nconst filtered = myComponent.byProps({ someBooleanProp: true }, { exact: true })\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbaruchvlz%2Fresq","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbaruchvlz%2Fresq","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbaruchvlz%2Fresq/lists"}