Giter VIP home page Giter VIP logo

Comments (8)

AlejandroSanchez90 avatar AlejandroSanchez90 commented on July 3, 2024 1

from react-to-print.

MatthewHerbst avatar MatthewHerbst commented on July 3, 2024

Hey there. I'm not sure I fully understand your concern. Are you trying to avoid showing all the pages in the print preview window opened by the browser? I don't think that's possible to my knowledge, though it's generally pretty performant.

In terms of generating the content, you can pass a Promise to the onBeforeGetContent prop so that will cause react-to-print to wait to print until you resolve the promise, and during that time you can generate the temporary content for printing that may not display on the main page.

Does that answer your question?

from react-to-print.

AlejandroSanchez90 avatar AlejandroSanchez90 commented on July 3, 2024

from react-to-print.

MatthewHerbst avatar MatthewHerbst commented on July 3, 2024

Ah. onBeforeGetContent is what you want I think. It would let you only render what you want to print when the print is happening, that way you don't need to have it rendered and hidden when not printing. Does that make sense?

from react-to-print.

AlejandroSanchez90 avatar AlejandroSanchez90 commented on July 3, 2024

from react-to-print.

AlejandroSanchez90 avatar AlejandroSanchez90 commented on July 3, 2024

Ah. onBeforeGetContent is what you want I think. It would let you only render what you want to print when the print is happening, that way you don't need to have it rendered and hidden when not printing. Does that make sense?

how would you use this? like you still needs to pass the ref to the content property right?

Edit: Is this how it's supposed to be used

function App() {
  const [print, setPrint] = useState(false);
  const printRef = useRef<HTMLDivElement | null>(null);
  return (
    <div>
      {print && <Template ref={printRef} />}
      <ReactToPrint
        trigger={() => <button>Print</button>}
        content={() => printRef.current}
        onBeforeGetContent={() => {
          setPrint(true);
          return Promise.resolve();
        }}
        onAfterPrint={() => {
          setPrint(false);
          return Promise.resolve();
        }}
      />
    </div>
  );
}

from react-to-print.

MatthewHerbst avatar MatthewHerbst commented on July 3, 2024

That's basically right! Recall that setting state is async though, so it's a bit more complicated to know when the state has actually been set:

function App() {
  const [print, setPrint] = useState(false);
  const onBeforeGetContentResolveRef = useRef();
  const printRef = useRef<HTMLDivElement | null>(null);

  useEffect(() => {
    // Wait for the state update
    if (onBeforeGetContentResolveRef.current && print === true) {
      onBeforeGetContentResolveRef.current(); // Resolve the promise
      onBeforeGetContentResolveRef.current = undefined;
    }
  }, [print])

  return (
    <div>
      {print && <Template ref={printRef} />}
      <ReactToPrint
        trigger={() => <button>Print</button>}
        content={() => printRef.current}
        onBeforeGetContent={() => {
          setPrint(true);
          return new Promise((resolve) => {
            onBeforeGetContentResolveRef.current = resolve;
          });
        }}
        onAfterPrint={() => {
          setPrint(false);
        }}
      />
    </div>
  );
}

In the old world you could do this with the callback function from seState that runs after the state is applied, but they didn't add that into the functional component/hook world unfortunately

from react-to-print.

MatthewHerbst avatar MatthewHerbst commented on July 3, 2024

@AlejandroSanchez90 circling back here, did the above work for you?

from react-to-print.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.