跳到主要内容

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