Skip to content

Commit 52ace4e

Browse files
authored
Merge pull request temando#80 from brendo/pure-components
Migrate to pure components where possible
2 parents aaf50b1 + d739a78 commit 52ace4e

File tree

16 files changed

+58
-56
lines changed

16 files changed

+58
-56
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
"jest": "^20.0.4",
6262
"json-loader": "^0.5.4",
6363
"node-sass": "^4.5.1",
64+
"react-addons-perf": "^15.4.2",
6465
"react-test-renderer": "^15.5.4",
6566
"sass-loader": "^6.0.3",
6667
"style-loader": "^0.16.0",

src/components/BodyContent/BodyContent.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@ export default class BodyContent extends Component {
1111
constructor (props) {
1212
super(props)
1313

14-
this.renderTabs = this.renderTabs.bind(this)
15-
this.renderSchema = this.renderSchema.bind(this)
16-
this.renderExamples = this.renderExamples.bind(this)
17-
1814
this.state = {
1915
tab: 'schema'
2016
}

src/components/BodySchema/BodySchema.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React, { Component } from 'react'
22
import createFragment from 'react-addons-create-fragment'
33
import classNames from 'classnames'
44
import PropTypes from 'prop-types'
5+
import isEqual from 'lodash/isEqual'
56

67
import Property from '../Property/Property'
78

@@ -11,15 +12,17 @@ export default class BodySchema extends Component {
1112
constructor (props) {
1213
super(props)
1314

14-
this.renderPropertyRow = this.renderPropertyRow.bind(this)
15-
this.renderSubsetProperties = this.renderSubsetProperties.bind(this)
1615
this.onClick = this.onClick.bind(this)
1716

1817
this.state = {
1918
expandedProp: []
2019
}
2120
}
2221

22+
shouldComponentUpdate (nextProps, nextState) {
23+
return !isEqual(nextState.expandedProp, this.state.expandedProp)
24+
}
25+
2326
render () {
2427
const { properties, styleVariation } = this.props
2528

src/components/CopyButton/CopyButton.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,13 @@ export default class CopyButton extends Component {
2727
}
2828

2929
onClick (e) {
30-
this.setState({
31-
...this.state,
32-
tooltip: 'Copied'
33-
})
30+
this.setState({ tooltip: 'Copied' })
3431

3532
this.props.onCopyClick(e)
3633
}
3734

3835
onMouseOver (e) {
39-
this.setState({
40-
...this.state,
41-
tooltip: this.props.tooltip
42-
})
36+
this.setState({ tooltip: this.props.tooltip })
4337
}
4438
}
4539

src/components/Description/Description.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import React, { Component } from 'react'
1+
import React, { PureComponent } from 'react'
22
import classNames from 'classnames'
33
import markdown from 'markdown-it'
44
import PropTypes from 'prop-types'
55
import './Description.scss'
66

77
const cm = markdown('commonmark')
88

9-
export default class Description extends Component {
9+
export default class Description extends PureComponent {
1010
render () {
1111
const { isInline, description } = this.props
1212
const text = {

src/components/Header/Header.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
import React, { Component } from 'react'
1+
import React, { PureComponent } from 'react'
22
import PropTypes from 'prop-types'
33

44
import Description from '../Description/Description'
55
import './Header.scss'
66

7-
export default class Header extends Component {
7+
export default class Header extends PureComponent {
88
render () {
99
const { title, description, version } = this.props
10+
1011
return (
1112
<header id='header'>
1213
<h1>{title}</h1>

src/components/Indicator/Indicator.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import React, { Component } from 'react'
1+
import React, { PureComponent } from 'react'
22
import classNames from 'classnames'
33
import PropTypes from 'prop-types'
44

55
import './Indicator.scss'
66

77
const arrow = require('./arrow.png')
88

9-
export default class Indicator extends Component {
9+
export default class Indicator extends PureComponent {
1010
render () {
1111
const { status, className } = this.props
1212

src/components/Method/Method.js

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { Component } from 'react'
1+
import React, { PureComponent } from 'react'
22
import PropTypes from 'prop-types'
33
import ScrollableAnchor from 'react-scrollable-anchor'
44

@@ -9,26 +9,16 @@ import Response from '../Response/Response'
99

1010
import './Method.scss'
1111

12-
export default class Method extends Component {
13-
constructor (props) {
14-
super(props)
15-
16-
this.renderParameters = this.renderParameters.bind(this)
17-
this.renderRequest = this.renderRequest.bind(this)
18-
this.renderResponses = this.renderResponses.bind(this)
19-
}
20-
12+
export default class Method extends PureComponent {
2113
render () {
2214
const { method } = this.props
23-
const parameters = method.parameters
24-
const request = method.request
25-
const responses = method.responses
15+
const { summary, description, parameters, request, responses } = method
2616

2717
return (
2818
<ScrollableAnchor id={method.link}>
2919
<div className='method'>
30-
<h3>{method.summary}</h3>
31-
{method.description && <Description description={method.description} />}
20+
<h3>{summary}</h3>
21+
{description && <Description description={description} />}
3222
{parameters && this.renderParameters(parameters)}
3323
{request && this.renderRequest(request)}
3424
{responses && this.renderResponses(responses)}

src/components/Navigation/Navigation.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React, { Component } from 'react'
22
import PropTypes from 'prop-types'
3+
import isEqual from 'lodash/isEqual'
34

45
import NavigationTag from '../NavigationTag/NavigationTag'
56

@@ -16,6 +17,13 @@ export default class Navigation extends Component {
1617
}
1718
}
1819

20+
shouldComponentUpdate (nextProps, nextState) {
21+
const isHashDiff = this.props.location.hash !== nextProps.location.hash
22+
const isTagsDiff = !isEqual(nextState.expandedTags, this.state.expandedTags)
23+
24+
return isHashDiff || isTagsDiff
25+
}
26+
1927
render () {
2028
const { navigation, location } = this.props
2129
const { expandedTags } = this.state

src/components/NavigationMethod/NavigationMethod.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import React, { Component } from 'react'
1+
import React, { PureComponent } from 'react'
22
import PropTypes from 'prop-types'
33
import classNames from 'classnames'
44

55
import './NavigationMethod.scss'
66

7-
export default class NavigationMethod extends Component {
7+
export default class NavigationMethod extends PureComponent {
88
render () {
99
const { method, isActive } = this.props
1010

src/components/NavigationTag/NavigationTag.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ export default class NavigationTag extends Component {
1313
this.handleClick = this.handleClick.bind(this)
1414
}
1515

16+
shouldComponentUpdate (nextProps, nextState) {
17+
const isHashDiff = this.props.location.hash !== nextProps.location.hash
18+
const isStatusDiff = this.props.status !== nextProps.status
19+
20+
return isHashDiff || isStatusDiff
21+
}
22+
1623
componentWillMount () {
1724
const { title, methods, location, onClick } = this.props
1825

src/components/Page/Page.js

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ export default class Page extends Component {
1313
const { definition, location } = this.props
1414

1515
if (!definition) {
16-
return ''
16+
return null
1717
}
1818

19-
const navigation = definition.navigation
20-
const services = definition.services
19+
const { navigation, services } = definition
20+
2121
return (
2222
<div className='page'>
2323
<Navigation navigation={navigation} location={location} />
@@ -28,14 +28,10 @@ export default class Page extends Component {
2828
version={definition.version}
2929
/>
3030
<ContentContainer>
31-
{services && services.map((service) => {
32-
return (
33-
<ServiceContainer
34-
key={service.title}
35-
service={service}
36-
/>
37-
)
38-
})}
31+
{services && services.map(
32+
(service) =>
33+
<ServiceContainer key={service.title} service={service} />
34+
)}
3935
</ContentContainer>
4036
</div>
4137
</div>

src/components/Property/Property.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { Component } from 'react'
1+
import React, { PureComponent } from 'react'
22
import classNames from 'classnames'
33
import PropTypes from 'prop-types'
44

@@ -8,7 +8,7 @@ import PropertyConstraints from '../PropertyConstraints/PropertyConstraints'
88

99
import './Property.scss'
1010

11-
export default class Property extends Component {
11+
export default class Property extends PureComponent {
1212
constructor (props) {
1313
super(props)
1414

src/components/Response/Response.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { Component } from 'react'
1+
import React, { PureComponent } from 'react'
22
import classNames from 'classnames'
33
import PropTypes from 'prop-types'
44

@@ -8,7 +8,7 @@ import Indicator from '../Indicator/Indicator'
88

99
import './Response.scss'
1010

11-
export default class Response extends Component {
11+
export default class Response extends PureComponent {
1212
constructor (props) {
1313
super(props)
1414

src/components/ServiceContainer/ServiceContainer.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
import React, { Component } from 'react'
1+
import React, { PureComponent } from 'react'
22
import ScrollableAnchor from 'react-scrollable-anchor'
33
import PropTypes from 'prop-types'
44

55
import Method from '../Method/Method'
66

77
// import './ServiceContainer.scss';
88

9-
export default class ServiceContainer extends Component {
9+
export default class ServiceContainer extends PureComponent {
1010
render () {
1111
const { service } = this.props
12-
const methods = service.methods
13-
const title = service.title
12+
const { title, methods } = service
1413

1514
return (
1615
<ScrollableAnchor id={title}>

yarn.lock

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4578,6 +4578,13 @@ react-addons-create-fragment@^15.5.4:
45784578
loose-envify "^1.3.1"
45794579
object-assign "^4.1.0"
45804580

4581+
react-addons-perf@^15.4.2:
4582+
version "15.4.2"
4583+
resolved "https://registry.yarnpkg.com/react-addons-perf/-/react-addons-perf-15.4.2.tgz#110bdcf5c459c4f77cb85ed634bcd3397536383b"
4584+
dependencies:
4585+
fbjs "^0.8.4"
4586+
object-assign "^4.1.0"
4587+
45814588
react-document-title@^2.0.2:
45824589
version "2.0.3"
45834590
resolved "https://registry.yarnpkg.com/react-document-title/-/react-document-title-2.0.3.tgz#bbf922a0d71412fc948245e4283b2412df70f2b9"

0 commit comments

Comments
 (0)