geongeorge / react-use-print

Detect and conditionally render for Print in react

Home Page:https://codesandbox.io/p/sandbox/determined-benz-4mnj6h

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

usePrint() for React

This is a simple react hook that lets you detect print and conditionally render components based on @media print.

Live Demo

Installation

npm install react-use-print

# or

yarn add react-use-print

Usage

import { usePrint } from "react-use-print";

export default function App() {
  const { isPrint, print } = usePrint();

  function printNow() {
    print();
  }

  return (
    <div>
      {isPrint ? <h1>Print</h1> : <h1>Screen</h1>}

      <button onClick={printNow}>Print</button>
    </div>
  );
}

Useful links

About

Detect and conditionally render for Print in react

https://codesandbox.io/p/sandbox/determined-benz-4mnj6h


Languages

Language:TypeScript 79.4%Language:JavaScript 11.2%Language:HTML 9.4%