Skip to content

Commit faad66b

Browse files
committed
Add displayName
1 parent e59d668 commit faad66b

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

README.md

+29
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ You can load styles in the way that you prefer but is important to keep in mind
6868
For example, this is a valid one `.⚛App`.
6969
So, pay attention that components' names and css selectors always match.
7070
This is particular important if you have css-modules that modifies the original names, or code obfuscation.
71+
7172
The first ones for example need a syntax like this:
7273

7374
```css
@@ -76,6 +77,28 @@ The first ones for example need a syntax like this:
7677
}
7778
```
7879

80+
And the second one, considering for example a minification process with webpack's UglifyJsPlugin (see [here](https://github.com/facebook/react/issues/4915) for more information),
81+
need a component with a displayName attribute like this:
82+
83+
```js
84+
class ComponentName extends React.Component {
85+
86+
static propTypes = {
87+
// propTypes...
88+
}
89+
90+
// this
91+
static displayName = 'ComponentName';
92+
93+
render() {
94+
// render here...
95+
}
96+
}
97+
98+
// or this
99+
// ComponentName.displayName = 'ComponentName';
100+
```
101+
79102

80103
### Adapting based on props
81104

@@ -84,6 +107,9 @@ If you want to set styles based on props, you can do it in 2 ways:
84107
- Set inline styles, as we can see in this example:
85108
```js
86109
class Button extends React.Component {
110+
111+
static displayName = 'Button';
112+
87113
render() {
88114
return (
89115
<button
@@ -103,6 +129,9 @@ class Button extends React.Component {
103129
import styles from './Button.css';
104130

105131
export default class Button extends React.Component {
132+
133+
static displayName = 'Button';
134+
106135
render() {
107136
return (
108137
<button

src/classInjector.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export default function initClassInjector() {
1111
const component = originalRender.apply(this);
1212
let result = component;
1313
if (component) {
14-
const classToInject = `⚛${this.constructor.name}`;
14+
const classToInject = `⚛${this.constructor.displayName || this.constructor.name}`;
1515
if (typeof component.type === 'string') {
1616
result = React.cloneElement(
1717
component,

test/classInjector.js

+28
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,32 @@ describe('Css class injector', () => {
8787
className: '⚛Foo',
8888
});
8989
});
90+
91+
it('should inject displayName', () => {
92+
//eslint-disable-next-line
93+
class Foo extends React.Component {
94+
static propTypes = {
95+
style: React.PropTypes.string,
96+
className: React.PropTypes.string,
97+
}
98+
99+
static displayName = 'Bar';
100+
101+
render() {
102+
return (
103+
<div {...this.props}>
104+
<p></p>
105+
</div>
106+
);
107+
}
108+
}
109+
110+
const renderer = ReactTestUtils.createRenderer();
111+
renderer.render(<Foo style="bar" />);
112+
const result = renderer.getRenderOutput();
113+
expect(result.props).toMatch({
114+
style: 'bar',
115+
className: '⚛Bar ',
116+
});
117+
});
90118
});

0 commit comments

Comments
 (0)