From 6081be5eb5e09d061d4f45f3ae78da8f59197a8d Mon Sep 17 00:00:00 2001 From: pawan_m02111 Date: Wed, 11 Dec 2019 16:42:43 +0530 Subject: [PATCH] fix issue with linkify default prop --- src/Hyperlink.js | 40 ++++++++++++++++------------------------ 1 file changed, 16 insertions(+), 24 deletions(-) diff --git a/src/Hyperlink.js b/src/Hyperlink.js index 05bcb43..b8790b7 100644 --- a/src/Hyperlink.js +++ b/src/Hyperlink.js @@ -12,26 +12,15 @@ import { } from 'react-native' import mdurl from 'mdurl' +const linkify = require('linkify-it')() + const textPropTypes = Text.propTypes || {} const { OS } = Platform class Hyperlink extends Component { - static getDerivedStateFromProps(nextProps, prevState) { - if (nextProps.linkify !== prevState.linkifyIt) { - return { - linkifyIt: nextProps.linkify - }; - } - - return null - } - - constructor(props){ + constructor(props) { super(props) - - this.state = { - linkifyIt: props.linkify || require('linkify-it')() - }; + this.state = { linkifyIt: props.linkify || linkify } } render() { @@ -114,10 +103,7 @@ class Hyperlink extends Component { } parse = component => { - let { - props: { children } = {}, - type: { displayName } = {}, - } = component || {} + let { props: { children } = {} } = component || {} if (!children) return component @@ -150,6 +136,12 @@ Hyperlink.propTypes = { onLongPress: PropTypes.func, } +Hyperlink.defaultProps = { linkify } + +Hyperlink.getDerivedStateFromProps = (nextProps, prevState) => (nextProps.linkify !== prevState.linkifyIt) + ? { linkifyIt: nextProps.linkify } + : null + export default class extends Component { constructor (props) { super(props) @@ -157,18 +149,18 @@ export default class extends Component { } handleLink (url) { - const urlObject = mdurl.parse(url); - urlObject.protocol = urlObject.protocol.toLowerCase(); + const urlObject = mdurl.parse(url) + urlObject.protocol = urlObject.protocol.toLowerCase() const normalizedURL = mdurl.format(urlObject) Linking.canOpenURL(normalizedURL) - .then(supported => supported && Linking.openURL(normalizedURL)); + .then(supported => supported && Linking.openURL(normalizedURL)) } render () { const onPress = this.handleLink || this.props.onPress - if (this.props.linkDefault) - return + if (this.props.linkDefault) + return return } }