Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typescript > Functional component > Example #401

Closed
michaelpeterlee opened this issue Aug 2, 2021 · 8 comments
Closed

Typescript > Functional component > Example #401

michaelpeterlee opened this issue Aug 2, 2021 · 8 comments

Comments

@michaelpeterlee
Copy link

React: 16.14.0

Typescript is not happy, below. Can someone please provide an example implementation.

image

image

@MatthewHerbst
Copy link
Owner

Hello. Can you show your code without the error boxes? Specifically, how did you define the generic that useContext/createContext takes?

@michaelpeterlee
Copy link
Author

michaelpeterlee commented Aug 2, 2021

const MyView: React.FC<IMyView> = (props) => {
  const componentRef = React.useRef();
  return React.useMemo(() => {
    <>
      <ReactToPrint trigger={() => (<div>PRINT</div>)} content={() => componentRef.current} />
      <div>Print this</div>
    </>
  });
}

@MatthewHerbst
Copy link
Owner

const componentRef = React.useRef(null); should fix the error I believe. This will make the value of componentRef.current null until it makes its way to ReactToPrint.

@michaelpeterlee
Copy link
Author

Thanks @MatthewHerbst, it compiles now however a subsequent error fires when printing:

There is nothing to print because the "content" prop returned "null". Please ensure "content" is renderable before allowing "react-to-print" to be called.

@MatthewHerbst
Copy link
Owner

That's a correct error. You never assign the ref to anything. You need to do: <div ref={componentRef}>Print this</div>

@michaelpeterlee
Copy link
Author

That is it @MatthewHerbst, thanks for the support and please remember to update the docs.

@MatthewHerbst
Copy link
Owner

@michaelpeterlee PRs are welcome 😄

MatthewHerbst pushed a commit that referenced this issue Sep 12, 2021
Explaining solutions for problems like in the issue #401 and #412
@dglitxh
Copy link

dglitxh commented Feb 26, 2022

If the component to be printed is wrapped in a div, the ref should be placed inside the div and not the component itself.
This worked for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants