Skip to content

Commit

Permalink
Migrate to pure components where possible, implement shouldComponentU…
Browse files Browse the repository at this point in the history
…pdate where possible
  • Loading branch information
Brendan Abbott committed Jun 1, 2017
1 parent 8579af5 commit f5ce2f2
Show file tree
Hide file tree
Showing 17 changed files with 60 additions and 58 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"jest": "^20.0.4",
"json-loader": "^0.5.4",
"node-sass": "^4.5.1",
"react-addons-perf": "^15.4.2",
"react-test-renderer": "^15.5.4",
"sass-loader": "^6.0.3",
"style-loader": "^0.16.0",
Expand Down
4 changes: 0 additions & 4 deletions src/components/BodyContent/BodyContent.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ export default class BodyContent extends Component {
constructor (props) {
super(props)

this.renderTabs = this.renderTabs.bind(this)
this.renderSchema = this.renderSchema.bind(this)
this.renderExamples = this.renderExamples.bind(this)

this.state = {
tab: 'schema'
}
Expand Down
7 changes: 5 additions & 2 deletions src/components/BodySchema/BodySchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { Component } from 'react'
import createFragment from 'react-addons-create-fragment'
import classNames from 'classnames'
import PropTypes from 'prop-types'
import isEqual from 'lodash/isEqual'

import Property from '../Property/Property'

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

this.renderPropertyRow = this.renderPropertyRow.bind(this)
this.renderSubsetProperties = this.renderSubsetProperties.bind(this)
this.onClick = this.onClick.bind(this)

this.state = {
expandedProp: []
}
}

shouldComponentUpdate (nextProps, nextState) {
return !isEqual(nextState.expandedProp, this.state.expandedProp)
}

render () {
const { properties, styleVariation } = this.props

Expand Down
4 changes: 2 additions & 2 deletions src/components/ContentContainer/ContentContainer.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import React, { Component } from 'react'
import React, { PureComponent } from 'react'
import PropTypes from 'prop-types'

import './ContentContainer.scss'

export default class ContentContainer extends Component {
export default class ContentContainer extends PureComponent {
render () {
return (
<div className='content-container'>
Expand Down
10 changes: 2 additions & 8 deletions src/components/CopyButton/CopyButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,13 @@ export default class CopyButton extends Component {
}

onClick (e) {
this.setState({
...this.state,
tooltip: 'Copied'
})
this.setState({ tooltip: 'Copied' })

this.props.onCopyClick(e)
}

onMouseOver (e) {
this.setState({
...this.state,
tooltip: this.props.tooltip
})
this.setState({ tooltip: this.props.tooltip })
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/components/Description/Description.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import React, { Component } from 'react'
import React, { PureComponent } from 'react'
import classNames from 'classnames'
import markdown from 'markdown-it'
import PropTypes from 'prop-types'
import './Description.scss'

const cm = markdown('commonmark')

export default class Description extends Component {
export default class Description extends PureComponent {
render () {
const { isInline, description } = this.props
const text = {
Expand Down
5 changes: 3 additions & 2 deletions src/components/Header/Header.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import React, { Component } from 'react'
import React, { PureComponent } from 'react'
import PropTypes from 'prop-types'

import Description from '../Description/Description'
import './Header.scss'

export default class Header extends Component {
export default class Header extends PureComponent {
render () {
const { title, description, version } = this.props

return (
<header id='header'>
<h1>{title}</h1>
Expand Down
4 changes: 2 additions & 2 deletions src/components/Indicator/Indicator.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import React, { Component } from 'react'
import React, { PureComponent } from 'react'
import classNames from 'classnames'
import PropTypes from 'prop-types'

import './Indicator.scss'

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

export default class Indicator extends Component {
export default class Indicator extends PureComponent {
render () {
const { status, className } = this.props

Expand Down
20 changes: 5 additions & 15 deletions src/components/Method/Method.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { Component } from 'react'
import React, { PureComponent } from 'react'
import PropTypes from 'prop-types'
import ScrollableAnchor from 'react-scrollable-anchor'

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

import './Method.scss'

export default class Method extends Component {
constructor (props) {
super(props)

this.renderParameters = this.renderParameters.bind(this)
this.renderRequest = this.renderRequest.bind(this)
this.renderResponses = this.renderResponses.bind(this)
}

export default class Method extends PureComponent {
render () {
const { method } = this.props
const parameters = method.parameters
const request = method.request
const responses = method.responses
const { summary, description, parameters, request, responses } = method

return (
<ScrollableAnchor id={method.link}>
<div className='method'>
<h3>{method.summary}</h3>
{method.description && <Description description={method.description} />}
<h3>{summary}</h3>
{description && <Description description={description} />}
{parameters && this.renderParameters(parameters)}
{request && this.renderRequest(request)}
{responses && this.renderResponses(responses)}
Expand Down
8 changes: 8 additions & 0 deletions src/components/Navigation/Navigation.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import isEqual from 'lodash/isEqual'

import NavigationTag from '../NavigationTag/NavigationTag'

Expand All @@ -16,6 +17,13 @@ export default class Navigation extends Component {
}
}

shouldComponentUpdate (nextProps, nextState) {
const isHashDiff = this.props.location.hash !== nextProps.location.hash
const isTagsDiff = !isEqual(nextState.expandedTags, this.state.expandedTags)

return isHashDiff || isTagsDiff
}

render () {
const { navigation, location } = this.props
const { expandedTags } = this.state
Expand Down
4 changes: 2 additions & 2 deletions src/components/NavigationMethod/NavigationMethod.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import React, { Component } from 'react'
import React, { PureComponent } from 'react'
import PropTypes from 'prop-types'
import classNames from 'classnames'

import './NavigationMethod.scss'

export default class NavigationMethod extends Component {
export default class NavigationMethod extends PureComponent {
render () {
const { method, isActive } = this.props

Expand Down
7 changes: 7 additions & 0 deletions src/components/NavigationTag/NavigationTag.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ export default class NavigationTag extends Component {
this.handleClick = this.handleClick.bind(this)
}

shouldComponentUpdate (nextProps, nextState) {
const isHashDiff = this.props.location.hash !== nextProps.location.hash
const isStatusDiff = this.props.status !== nextProps.status

return isHashDiff || isStatusDiff
}

componentWillMount () {
const { title, methods, location, onClick } = this.props

Expand Down
18 changes: 7 additions & 11 deletions src/components/Page/Page.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ export default class Page extends Component {
const { definition, location } = this.props

if (!definition) {
return ''
return null
}

const navigation = definition.navigation
const services = definition.services
const { navigation, services } = definition

return (
<div className='page'>
<Navigation navigation={navigation} location={location} />
Expand All @@ -28,14 +28,10 @@ export default class Page extends Component {
version={definition.version}
/>
<ContentContainer>
{services && services.map((service) => {
return (
<ServiceContainer
key={service.title}
service={service}
/>
)
})}
{services && services.map(
(service) =>
<ServiceContainer key={service.title} service={service} />
)}
</ContentContainer>
</div>
</div>
Expand Down
4 changes: 2 additions & 2 deletions src/components/Property/Property.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { Component } from 'react'
import React, { PureComponent } from 'react'
import classNames from 'classnames'
import PropTypes from 'prop-types'

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

import './Property.scss'

export default class Property extends Component {
export default class Property extends PureComponent {
constructor (props) {
super(props)

Expand Down
4 changes: 2 additions & 2 deletions src/components/Response/Response.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { Component } from 'react'
import React, { PureComponent } from 'react'
import classNames from 'classnames'
import PropTypes from 'prop-types'

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

import './Response.scss'

export default class Response extends Component {
export default class Response extends PureComponent {
constructor (props) {
super(props)

Expand Down
7 changes: 3 additions & 4 deletions src/components/ServiceContainer/ServiceContainer.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import React, { Component } from 'react'
import React, { PureComponent } from 'react'
import ScrollableAnchor from 'react-scrollable-anchor'
import PropTypes from 'prop-types'

import Method from '../Method/Method'

// import './ServiceContainer.scss';

export default class ServiceContainer extends Component {
export default class ServiceContainer extends PureComponent {
render () {
const { service } = this.props
const methods = service.methods
const title = service.title
const { title, methods } = service

return (
<ScrollableAnchor id={title}>
Expand Down
7 changes: 7 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4578,6 +4578,13 @@ react-addons-create-fragment@^15.5.4:
loose-envify "^1.3.1"
object-assign "^4.1.0"

react-addons-perf@^15.4.2:
version "15.4.2"
resolved "https://registry.yarnpkg.com/react-addons-perf/-/react-addons-perf-15.4.2.tgz#110bdcf5c459c4f77cb85ed634bcd3397536383b"
dependencies:
fbjs "^0.8.4"
object-assign "^4.1.0"

react-document-title@^2.0.2:
version "2.0.3"
resolved "https://registry.yarnpkg.com/react-document-title/-/react-document-title-2.0.3.tgz#bbf922a0d71412fc948245e4283b2412df70f2b9"
Expand Down

0 comments on commit f5ce2f2

Please sign in to comment.