LoginSignup
0
1

More than 1 year has passed since last update.

【React】文字数を制限するカスタムフックの作り方

Last updated at Posted at 2022-05-15

文字数が多いとき、...とするときに使えるカスタムフックを作りました

export const useMakeString = (content: string, maxContentLength: number) => {
  if (content.length > maxContentLength) {
    return content.substring(0, maxContentLength) + "...";
  }
  return content;
};

実際の使用方法

export const sample = () => {
  const title = "これは長めのタイトル文です。表示するときにはみ出てしまいます。";
 const displayTitle = useMakeString(title, 15);
  return <div>{displayTitle }</div>;
};

2022/05/19追記

こんなことしなくてもcss(tailwind.css)で下のように書けばやりたいことが実現できる

 <div className="truncate">一行を超える文章</div>
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1