From c21d71f9bcffc03d1021b98dd15ac392ff283874 Mon Sep 17 00:00:00 2001 From: Ben Woosley Date: Thu, 9 Jun 2016 15:28:44 -0700 Subject: [PATCH 1/4] Add propTypes --- src/index.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/index.js b/src/index.js index 46c7336..7826ae9 100644 --- a/src/index.js +++ b/src/index.js @@ -1,9 +1,21 @@ 'use strict'; import React from 'react'; +import PropTypes from 'prop-types'; import Markdown from 'remarkable'; class Remarkable extends React.Component { + static propTypes = { + container: PropTypes.string, + options: PropTypes.object, + source: PropTypes.string, + children: PropTypes.array + } + + static defaultProps = { + container: 'div', + options: {} + } render() { var Container = this.props.container; From d91820931f42a6d56423970b359513b11f333110 Mon Sep 17 00:00:00 2001 From: Ben Woosley Date: Thu, 9 Jun 2016 15:50:31 -0700 Subject: [PATCH 2/4] Don't update the component if the source has not changed If there are no children, then the output could not have changed in this case. --- src/index.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/index.js b/src/index.js index 7826ae9..6a7e838 100644 --- a/src/index.js +++ b/src/index.js @@ -27,6 +27,18 @@ class Remarkable extends React.Component { ); } + shouldComponentUpdate(nextProps, nextState) { + if (nextProps.options !== this.props.options) { + return true; + } + else if (this.props.source) { + return this.props.source !== nextProps.source; + } + else { + return true; + } + } + componentWillUpdate(nextProps, nextState) { if (nextProps.options !== this.props.options) { this.md = new Markdown(nextProps.options); From 1214e3c1aa937aed6fdbef680e9f029b33c83d52 Mon Sep 17 00:00:00 2001 From: Ben Woosley Date: Thu, 9 Jun 2016 16:00:47 -0700 Subject: [PATCH 3/4] Also don't update if there is one unchanged string child This case is equivalent to the unchanged source case. --- src/index.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/index.js b/src/index.js index 6a7e838..589a3af 100644 --- a/src/index.js +++ b/src/index.js @@ -34,6 +34,9 @@ class Remarkable extends React.Component { else if (this.props.source) { return this.props.source !== nextProps.source; } + else if (React.Children.count(this.props.children) === 1 && React.Children.count(nextProps.children) === 1) { + return (typeof this.props.children === 'string') && this.props.children !== nextProps.children; + } else { return true; } From 8259b974c314505ac654d993a09176fc27188e4a Mon Sep 17 00:00:00 2001 From: Ben Woosley Date: Tue, 26 Sep 2017 14:32:10 -0700 Subject: [PATCH 4/4] Expand children propTypes to support single children --- src/index.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 589a3af..3e4bca9 100644 --- a/src/index.js +++ b/src/index.js @@ -9,7 +9,10 @@ class Remarkable extends React.Component { container: PropTypes.string, options: PropTypes.object, source: PropTypes.string, - children: PropTypes.array + children: PropTypes.oneOfType([ + PropTypes.arrayOf(PropTypes.node), + PropTypes.node + ]) } static defaultProps = {