跳至主要内容

useTextSelection

跟蹤用户选中的文字

Usage

即時編輯器

function Demo() {
  const selection = useTextSelection();

  return (
    <div style={{ padding: 40 }}>
      <p>
        在這裡或頁面上的任何地方選擇一些文本,它將顯示在下面
      </p>

      <div>選中的文本: {selection?.toString()}</div>
    </div>
  );
};

render(<Demo/>)
結果
Error: Minified React error #185; visit https://react.dev/errors/185 for the full message or use the non-minified dev environment for full errors and additional helpful warnings.

API

useTextSelection

Returns

Selection | null: 選擇的文本對象

Arguments

ads via Carbon