Skip to content

Commit e917e17

Browse files
committed
fixed issue where markdown rendherer would not change styles because props would not be reupdated
1 parent fca065a commit e917e17

File tree

2 files changed

+106
-51
lines changed

2 files changed

+106
-51
lines changed

example/App.js

Lines changed: 51 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
import React from 'react';
2-
import { StyleSheet, Text, View, ScrollView } from 'react-native';
1+
import React, { Component } from 'react';
2+
import { StyleSheet, Text, View, ScrollView, Picker } from 'react-native';
33
import Markdown, {
4-
AstRenderer, styles, renderRules,
5-
getUniqueID
4+
AstRenderer,
5+
styles,
6+
renderRules,
7+
getUniqueID,
68
} from 'react-native-markdown-renderer';
79

810
const markdownText = `
@@ -175,32 +177,57 @@ This is a text. Click [here](https://google.com) to open a link. Let's add some
175177
/**
176178
* i'm overriding the default h1 render function.
177179
*/
178-
const renderer = new AstRenderer({
179-
...renderRules,
180-
h1: (node, children, parents) => {
181-
return (
182-
<Text key={getUniqueID()} style={{ backgroundColor: 'red' }}>{children}</Text>
183-
);
184-
},
185-
// added custom block element defined by plugin
186-
block: (node, children, parents) => {
187-
return (
188-
<Text key={getUniqueID()} style={{ backgroundColor: 'green' }}>{children}</Text>
189-
);
180+
const renderer = new AstRenderer(
181+
{
182+
...renderRules,
183+
h1: (node, children, parents) => {
184+
return <Text key={getUniqueID()} style={{ backgroundColor: 'red' }}>{children}</Text>;
185+
},
186+
// added custom block element defined by plugin
187+
block: (node, children, parents) => {
188+
return <Text key={getUniqueID()} style={{ backgroundColor: 'green' }}>{children}</Text>;
189+
},
190190
},
191-
}, styles);
191+
styles
192+
);
193+
194+
export default class App extends Component {
195+
state = {
196+
view: 0,
197+
};
198+
199+
list = [{ description: 'custom renderer' }, { description: 'custom style sheet' }];
200+
201+
getView(value) {
202+
203+
console.warn(value);
204+
switch (value) {
205+
case 0: {
206+
return <Markdown plugins={[]} renderer={renderer.render} children={markdownText} />;
207+
}
208+
default: {
209+
return (
210+
<Markdown># Text</Markdown>
211+
);
212+
}
213+
}
214+
}
192215

193-
export default class App extends React.Component {
194216
render() {
195-
217+
218+
196219

197220
return (
198-
<View style={styles.container}>
221+
<View style={styleSheet.container}>
222+
<Picker
223+
selectedValue={this.state.view}
224+
onValueChange={(itemValue, itemIndex) => this.setState({ view: itemIndex })}
225+
>
226+
{this.list.map((val, index) => <Picker.Item label={val.description} value={index} />)}
227+
</Picker>
199228

200229
<ScrollView>
201-
<Text>Markdown</Text>
202-
<Text>--------</Text>
203-
<Markdown plugins={[]} renderer={renderer.render} children={markdownText} />
230+
{this.getView(this.state.view)}
204231
</ScrollView>
205232
</View>
206233
);
@@ -210,7 +237,6 @@ export default class App extends React.Component {
210237
const styleSheet = StyleSheet.create({
211238
container: {
212239
flex: 1,
213-
backgroundColor: '#ffcc00',
214-
paddingTop: 20,
240+
marginTop: 20,
215241
},
216242
});

index.js

Lines changed: 55 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ export {
3535
styles,
3636
};
3737

38+
/**
39+
* react-native-markdown-renderer
40+
*/
3841
export default class Markdown extends Component {
3942
/**
4043
* Definition of the prop types
@@ -77,7 +80,7 @@ export default class Markdown extends Component {
7780
renderer: null,
7881
rules: null,
7982
plugins: [],
80-
style: {},
83+
style: null,
8184
markdownit: MarkdownIt({
8285
typographer: true,
8386
}),
@@ -104,11 +107,8 @@ export default class Markdown extends Component {
104107
return false;
105108
}
106109

107-
/**
108-
*
109-
*/
110-
componentWillMount() {
111-
const { renderer, rules, style, plugins, markdownit } = this.props;
110+
updateSettings(props = this.props) {
111+
const { renderer, rules, style, plugins, markdownit } = props;
112112

113113
if (renderer && rules) {
114114
console.warn(
@@ -122,39 +122,68 @@ export default class Markdown extends Component {
122122
);
123123
}
124124

125+
// these checks are here to prevent extra overhead.
125126
if (renderer) {
126127
if (typeof renderer === 'function') {
127-
this.renderer = {
128-
render: renderer,
129-
};
128+
if (!this.renderer || this.renderer.render !== renderer) {
129+
this.renderer = {
130+
render: renderer,
131+
};
132+
}
130133
} else if (renderer instanceof AstRenderer) {
131-
this.renderer = renderer;
134+
if (this.renderer !== renderer) {
135+
this.renderer = renderer;
136+
}
132137
} else {
133138
throw new Error(
134139
'Provided renderer is not compatible with function or AstRenderer. please change'
135140
);
136141
}
137142
} else {
138-
this.renderer = new AstRenderer(
139-
{
140-
...renderRules,
141-
...(rules || {}),
142-
},
143-
{
144-
...styles,
145-
...style,
146-
}
147-
);
143+
if (
144+
!this.renderer ||
145+
this.props.renderer ||
146+
this.props.rules !== rules ||
147+
this.props.style !== style
148+
) {
149+
this.renderer = new AstRenderer(
150+
{
151+
...renderRules,
152+
...(rules || {}),
153+
},
154+
{
155+
...styles,
156+
...style,
157+
}
158+
);
159+
}
148160
}
149161

150-
let md = markdownit;
151-
if (plugins && plugins.length > 0) {
152-
plugins.forEach(plugin => {
153-
md = md.use.apply(md, plugin.toArray());
154-
});
162+
if (
163+
!this.markdownParser ||
164+
this.props.markdownit !== markdownit ||
165+
plugins !== this.props.plugins
166+
) {
167+
let md = markdownit;
168+
if (plugins && plugins.length > 0) {
169+
plugins.forEach(plugin => {
170+
md = md.use.apply(md, plugin.toArray());
171+
});
172+
}
173+
174+
this.markdownParser = md;
155175
}
176+
}
177+
178+
/**
179+
*
180+
*/
181+
componentWillMount() {
182+
this.updateSettings(this.props);
183+
}
156184

157-
this.markdownParser = md;
185+
componentWillReceiveProps(nextProps) {
186+
this.updateSettings(nextProps);
158187
}
159188

160189
getCopyFromChildren(children = this.props.children) {

0 commit comments

Comments
 (0)