read-more-and-less
v1.0.1
Published
A react component to show "Read More" or "Read Less" button after text based on character limit.
Downloads
261
Readme
Introduction - "Read More / Read Less React Component"
This is an updated iteration of react-read-more-less
"Read More And Less" is react component, which lets you to truncate text based on specified character limit and show a link to "show more" or "show less" of the text.
Installation
npm i read-more-and-less
Import component
// Default import
import ReadMoreAndLess from "read-more-and-less";
// or Named import
import { ReadMoreAndLess } from "read-more-and-less";
Basic Usage
function Example() {
return (
<ReadMoreAndLess
text="Lorem ipsum dolor sit amet consectetur adipisicing elit. Aliquam expedita placeat cumque ullam fuga possimus sunt soluta voluptatibus blanditiis consequatur! Magni, maiores necessitatibus repudiandae totam facere officia iste ducimus vitae."
charLimit={30}
/>
);
}
Props
| | Description | Type | Required | Default Value |
| ----------------- | ------------------------------------------------------------------------------------------------- | --------- | -------- | ------------- |
| charLimit
| The character limit where you want your text to be truncated | number
| No | 150
|
| readMoreText
| The text you want to see in the "Read More" clickable link | string
| No | Read More
|
| readLessText
| The text you want to see in the "Read Less" clickable link | string
| No | Read Less
|
| stopPropogation
| If true
then event.stopPropagation()
is triggered on all Read More
/ Read Less
link click | boolean
| No | true
|
| preventDefault
| If true
then event.preventDefault()
is triggered on all Read More
/ Read Less
link click | boolean
| No | true
|
| text
| The text you want to be truncated | string
| Yes | N/A |
Notes on the original component
The original component itself still works, but it is using outdated dependecies and react class component. In this version, I have updated the dependencies and also implemented react function components.
The build process of the original component was also including react and webpack into its bundle. This was causing some extra load on one of the websites I was working on. In this iteration, I've tried to reduce the bundle size by only bundling the component file itself.