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 |
|
10 | You can use it as a babel-plugin or as the runtime function, or both.
|
11 |
|
12 | ### babel-plugin
|
13 |
|
14 | Add `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 |
|
31 | Use `select` function to build any css selector by React Components.
|
32 |
|
33 | #### Runtime (node.js)
|
34 |
|
35 | Use `resolve` or `resolveBy` functions to get Components' selector.
|
36 |
|
37 | ```jsx
|
38 | const {resolve} = require('reselector')
|
39 |
|
40 | const {MyComponent} = resolve(require.resolve('./MyComponent'))
|
41 | const {MyButton} = resolve(require.resolve('./MyButton'))
|
42 |
|
43 | /**
|
44 | * [data-dadad] [data-czczx]
|
45 | */
|
46 | console.log(select`${MyComponent} ${MyButton}`)
|
47 |
|
48 | /**
|
49 | * .myClassName > [data-czczx]
|
50 | */
|
51 | console.log(select`.myClassName > ${MyButton}`)
|
52 | ```
|
53 |
|
54 | ```jsx
|
55 | const {resolveBy} = require('reselector')
|
56 |
|
57 | const resolve = resolveBy(require.resolve)
|
58 |
|
59 | const {MyComponent} = resolve('./MyComponent')
|
60 | const {MyButton} = resolve('./MyButton')
|
61 |
|
62 | /**
|
63 | * [data-dadad] [data-czczx]
|
64 | */
|
65 | console.log(select`${MyComponent} ${MyButton}`)
|
66 |
|
67 | /**
|
68 | * .myClassName > [data-czczx]
|
69 | */
|
70 | console.log(select`.myClassName > ${MyButton}`)
|
71 | ```
|
72 |
|
73 | If 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
|
76 | import {select} from 'reselector'
|
77 |
|
78 | import MyComponent from './MyComponent'
|
79 | import MyButton from './MyButton'
|
80 |
|
81 | /**
|
82 | * [data-dadad] [data-czczx]
|
83 | */
|
84 | console.log(select`${MyComponent} ${MyButton}`)
|
85 |
|
86 | /**
|
87 | * .myClassName > [data-czczx]
|
88 | */
|
89 | console.log(select`.myClassName > ${MyButton}`)
|
90 |
|
91 | ```
|
92 |
|
93 | ## How it works
|
94 |
|
95 | This 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.
|