UNPKG

2.51 kBMarkdownView Raw
1# reselector
2
3[![Travis branch](https://img.shields.io/travis/lttb/reselector/master.svg?style=flat)](https://travis-ci.org/lttb/reselector)
4[![Coverage Status branch](https://img.shields.io/coveralls/lttb/reselector/master.svg?style=flat)](https://img.shields.io/coveralls/lttb/reselector/master.svg?branch=master)
5[![npm version](https://img.shields.io/npm/v/reselector.svg?style=flat)](https://www.npmjs.com/package/reselector)
6[![npm license](https://img.shields.io/npm/l/reselector.svg?style=flat)](https://www.npmjs.com/package/reselector)
7
8## Usage
9
10You can use it as a babel-plugin or as the runtime function, or both.
11
12### babel-plugin
13
14Add `reselector` to the plugin list in `.babelrc` for your client code. For example:
15
16```js
17{
18 presets: ['react'],
19 env: {
20 test: {
21 plugins: [
22 'reselector/babel',
23 ],
24 },
25 },
26}
27```
28
29### Find Components in the DOM
30
31Use `select` function to build any css selector by React Components.
32
33#### Runtime (node.js)
34
35Use `resolve` or `resolveBy` functions to get Components' selector.
36
37```jsx
38const {resolve} = require('reselector')
39
40const {MyComponent} = resolve(require.resolve('./MyComponent'))
41const {MyButton} = resolve(require.resolve('./MyButton'))
42
43/**
44 * [data-dadad] [data-czczx]
45 */
46console.log(select`${MyComponent} ${MyButton}`)
47
48/**
49 * .myClassName > [data-czczx]
50 */
51console.log(select`.myClassName > ${MyButton}`)
52```
53
54```jsx
55const {resolveBy} = require('reselector')
56
57const resolve = resolveBy(require.resolve)
58
59const {MyComponent} = resolve('./MyComponent')
60const {MyButton} = resolve('./MyButton')
61
62/**
63 * [data-dadad] [data-czczx]
64 */
65console.log(select`${MyComponent} ${MyButton}`)
66
67/**
68 * .myClassName > [data-czczx]
69 */
70console.log(select`.myClassName > ${MyButton}`)
71```
72
73If you have a change to transpile components with this plugin for your unit tests/autotests, you can import React Component as is.
74
75```jsx
76import {select} from 'reselector'
77
78import MyComponent from './MyComponent'
79import MyButton from './MyButton'
80
81/**
82 * [data-dadad] [data-czczx]
83 */
84console.log(select`${MyComponent} ${MyButton}`)
85
86/**
87 * .myClassName > [data-czczx]
88 */
89console.log(select`.myClassName > ${MyButton}`)
90
91```
92
93## How it works
94
95This plugin tries to find all React Component declarations and to add `data-{hash}` attribute with the uniq hash-id to the Component's root node. It also saves this hash as the static property for the Component, so `get` function uses this property to build a selector.