Skip to content

Commit

Permalink
Solved a warn issue with shouldComponentUpdate and PureComponent.
Browse files Browse the repository at this point in the history
PureComponent cannot be used when declaring shouldComponentUpdate. Changed to React.Component.
  • Loading branch information
alvaromb committed Aug 29, 2017
1 parent c026036 commit 43aa91a
Showing 1 changed file with 32 additions and 24 deletions.
56 changes: 32 additions & 24 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* @flow */

import React, { PureComponent } from 'react'
import React, { Component } from 'react'
import { View } from 'react-native'
import SimpleMarkdown from 'simple-markdown'
import _ from 'lodash'
Expand All @@ -12,58 +12,67 @@ type Props = {
children?: string,
rules: Object,
whitelist: Array,
blacklist: Array,
blacklist: Array
}

type DefaultProps = Props & {
children: string,
children: string
}

class Markdown extends PureComponent<DefaultProps, Props, void> {
class Markdown extends Component<DefaultProps, Props, void> {
static defaultProps = {
styles: initialStyles,
children: '',
rules: {},
whitelist: [],
blacklist: [],
blacklist: []
}

/** Post processes rules to strip out unwanted styling options
* while keeping the default 'paragraph' and 'text' rules
*/
_postProcessRules = (preRules: Array<string>): Array<string> => {
const defaultRules = ['paragraph', 'text']
if (this.props.whitelist.length) {
return _.pick(preRules, _.concat(this.props.whitelist, defaultRules))
}
else if (this.props.blacklist.length) {
return _.omit(preRules, _.pullAll(this.props.blacklist, defaultRules))
}
else {
return preRules
}
const defaultRules = ['paragraph', 'text']
if (this.props.whitelist.length) {
return _.pick(preRules, _.concat(this.props.whitelist, defaultRules))
} else if (this.props.blacklist.length) {
return _.omit(preRules, _.pullAll(this.props.blacklist, defaultRules))
} else {
return preRules
}
}

_renderContent = (children: string): React$Element<any> => {
try {
const mergedStyles = Object.assign(initialStyles, this.props.styles)
const rules = this._postProcessRules(_.merge({}, SimpleMarkdown.defaultRules, initialRules(mergedStyles), this.props.rules))
const rules = this._postProcessRules(
_.merge(
{},
SimpleMarkdown.defaultRules,
initialRules(mergedStyles),
this.props.rules
)
)
const child = Array.isArray(this.props.children)
? this.props.children.join('')
: this.props.children
const blockSource = child + '\n\n'
const tree = SimpleMarkdown.parserFor(rules)(blockSource, { inline: false })
return SimpleMarkdown.reactFor(SimpleMarkdown.ruleOutput(rules, 'react'))(tree)
} catch(errors) {
const tree = SimpleMarkdown.parserFor(rules)(blockSource, {
inline: false
})
return SimpleMarkdown.reactFor(SimpleMarkdown.ruleOutput(rules, 'react'))(
tree
)
} catch (errors) {
this.props.errorHandler
? this.props.errorHandler(errors,children)
? this.props.errorHandler(errors, children)
: console.error(errors)
}
}

shouldComponentUpdate = (nextProps: Props): boolean => (
this.props.children !== nextProps.children || this.props.styles !== nextProps.styles
)
shouldComponentUpdate = (nextProps: Props): boolean =>
this.props.children !== nextProps.children ||
this.props.styles !== nextProps.styles

render() {
return (
Expand All @@ -72,7 +81,6 @@ class Markdown extends PureComponent<DefaultProps, Props, void> {
</View>
)
}

}

export default Markdown

0 comments on commit 43aa91a

Please sign in to comment.