Created
March 4, 2019 06:15
-
-
Save trevorblades/107070003dfc9d89ef5d65d7b556f6b6 to your computer and use it in GitHub Desktop.
ReadMoreLess
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, {Component} from 'react'; | |
import PropTypes from 'prop-types'; | |
export default class ReadMoreLess extends Component { | |
static propTypes = { | |
text: PropTypes.string.isRequired | |
}; | |
state = { | |
expanded: false | |
}; | |
toggle = () => { | |
this.setState(prevState => ({ | |
expanded: !prevState.expanded | |
})); | |
}; | |
render() { | |
const text = this.state.expanded | |
? this.props.text | |
: this.props.text.slice(0, 30); | |
return ( | |
<div> | |
{text} | |
{!this.state.expanded ? ( | |
<span> | |
... <a onClick={this.toggle}>Read more</a> | |
</span> | |
) : ( | |
<span> | |
{' '} | |
<a onClick={this.toggle}>Read less</a> | |
</span> | |
)} | |
</div> | |
); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<ReadMoreLess text="Keyholder allows API developers to focus on building great APIs by providing a simple and free way to generate and authenticate API keys for their services." /> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment