truncateText
Truncate text to a maximum length with ellipsis.
Installation
npx react-usekit@latest init
Select: utils → TypeScript or JavaScript → path -> truncateText
Usage
import { truncateText } from "./utils/truncateText";
function App() {
const longText = "This is a very long text that needs to be truncated";
const shortText = truncateText(longText, 20);
return (
<div>
<p>Original: {longText}</p>
<p>Truncated: {shortText}</p>
</div>
);
}
API
Parameters
Parameter | Type | Description |
---|---|---|
text | string | The text to truncate |
maxLength | number | Maximum length before truncation |
Returns
Type | Description |
---|---|
string | The truncated text with ellipsis if needed |