Reputation: 371
I need to add a "read more" link after my paragraph reaches 250 characters..got many solutions using javascript but i am unable to do it with reactjs. Help in any form will be great! thanks Example : text text text texttext texttext texttext texttext texttext texttext texttext text...Read more
Upvotes: 12
Views: 45828
Reputation: 15345
If I understand correctly, the difference from what you could see online is that you don't want to show a Read more
button if the text has less than 250 characters.
It would be great if you could share what you already have, but just in case, here is a prototype:
class LongText extends Component {
state = { showAll: false }
showMore = () => this.setState({showAll: true});
showLess = () => this.setState({showAll: false});
render() {
const {content, limit} = this.props;
const {showAll} = this.state;
if(content.length<=limit) {
// there is nothing more to show
return <div>{content}<div>
}
if(showAll) {
// We show the extended text and a link to reduce it
return <div>
{content}
<a onClick={this.showLess}>Read less</a>
</div>
}
// In the final case, we show a text with ellipsis and a `Read more` button
const toShow = content.substring(0,limit)+"...";
return <div>
{toShow}
<span onClick={this.showMore}>Read more</a>
</div>
}
}
EDIT: with hooks
const {useState} = React;
const LongText = ({ content,limit}) => {
const [showAll, setShowAll] = useState(false);
const showMore = () => setShowAll(true);
const showLess = () => setShowAll(false);
if (content.length <= limit) {
// there is nothing more to show
return <div>{content}</div>
}
if (showAll) {
// We show the extended text and a link to reduce it
return <div>
{content}
<button onClick={showLess}>Read less</button>
</div>
}
// In the final case, we show a text with ellipsis and a `Read more` button
const toShow = content.substring(0, limit) + "...";
return <div>
{toShow}
<button onClick={showMore}>Read more</button>
</div>
}
const App = () => <div>
<LongText content = "Short text" limit = {10}/>
<LongText content = "Very long text, very very long" limit = {10} />
</div>
ReactDOM.render( <App/>,document.getElementById('react'));
button {
margin-left: 4px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.1/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.1/umd/react-dom.production.min.js"></script>
<div id="react"></div>
Upvotes: 17
Reputation: 2458
A simpler and more expressive example than the others posted, using ES6:
// Outside component
const MAX_LENGTH = 250;
render() {
const { text } = this.props;
return (
<div>
{text.length > MAX_LENGTH ?
(
<div>
{`${text.substring(0, MAX_LENGTH)}...`}<a href="#">Read more</a>
</div>
) :
<p>{text}</p>
}
</div>
);
}
Here's a fiddle demonstrating this.
Upvotes: 10
Reputation: 682
You can still use the answer from Implementing a Read More link in React.js and load the reduced text with the substr()
method like this:
var component = React.createClass({
getInitialState: function() {
return {
expanded: false,
myText: 'bla bla bla'
};
},
expandedText: function() {
this.setState({
expanded: true
});
},
getMoreTextDiv = function() {
if (this.state.expanded) {
return myText;
} else {
return myText.substr(0, 250);
}
}
render: function() {
var expandedDiv = this.getMoreTextDiv();
return (
<div>
<a onClick={ this.expandedText }>Read more</a>
{ expandedDiv }
</div>
);
}
});
Upvotes: 2