Skip to content

Commit d1384fa

Browse files
committed
Add displayName option to connect()
1 parent 253ce8b commit d1384fa

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

docs/api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ Instead, it *returns* a new, connected component class, for you to use.
7171
* [`options`] *(Object)* If specified, further customizes the behavior of the connector.
7272
* [`pure = true`] *(Boolean)*: If true, implements `shouldComponentUpdate` and shallowly compares the result of `mergeProps`, preventing unnecessary updates, assuming that the component is a “pure” component and does not rely on any input or state other than its props and the selected Redux store’s state. *Defaults to `true`.*
7373
* [`withRef = false`] *(Boolean)*: If true, stores a ref to the wrapped component instance and makes it available via `getWrappedInstance()` method. *Defaults to `false`.*
74+
* [`displayName = 'Connect'`] *(String)*: Custom display name for the generated wrapper component. This can been seen in the React Devtools and in warning messages. *Defaults to `'Connect'`.*
7475

7576
#### Returns
7677

src/components/connect.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,14 @@ export default function connect(mapStateToProps, mapDispatchToProps, mergeProps,
4646
}
4747

4848
const finalMergeProps = mergeProps || defaultMergeProps
49-
const { pure = true, withRef = false } = options
49+
const { pure = true, withRef = false, displayName = 'Connect' } = options
5050
const checkMergedEquals = pure && finalMergeProps !== defaultMergeProps
5151

5252
// Helps track hot reloading.
5353
const version = nextVersion++
5454

5555
return function wrapWithConnect(WrappedComponent) {
56-
const connectDisplayName = `Connect(${getDisplayName(WrappedComponent)})`
56+
const connectDisplayName = `${displayName}(${getDisplayName(WrappedComponent)})`
5757

5858
function checkStateShape(props, methodName) {
5959
if (!isPlainObject(props)) {

test/components/connect.spec.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1868,5 +1868,16 @@ describe('React', () => {
18681868

18691869
ReactDOM.unmountComponentAtNode(div)
18701870
})
1871+
1872+
it('should allow custom displayName', () => {
1873+
@connect(null, null, null, {displayName: 'Custom'})
1874+
class MyComponent extends React.Component {
1875+
render() {
1876+
return <div></div>
1877+
}
1878+
}
1879+
1880+
expect(MyComponent.displayName).toEqual('Custom(MyComponent)')
1881+
})
18711882
})
18721883
})

0 commit comments

Comments
 (0)