Truncate Function
This Truncate
Function is used to
Truncate the huge contents.
Truncate
truncate.ts
export const truncate = (text = '', max = 1) => {
const arr = text.trim().split(' ');
return arr.length > max ? arr.splice(0, max).join(' ') + '...' : text;
}
Usage
BlogCard.tsx
function BlogCard() {
const content = "HTML conents responsed back from Server Here...";
return (
<div
dangerouslySetInnerHTML={{
__html: content.length > 100
? truncate(content?.slice(0, 60)) + '...'
: truncate(content)
}}
/>
)
}