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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trigger does nothing if it returns a React component #248

Closed
laurpaik-zz opened this issue May 13, 2020 · 2 comments
Closed

Trigger does nothing if it returns a React component #248

laurpaik-zz opened this issue May 13, 2020 · 2 comments

Comments

@laurpaik-zz
Copy link

https://codesandbox.io/s/react-to-print-uhwxj

Heya 馃憢 see above sandbox
I see you do clone a component at least shallowly https://github.com/gregnb/react-to-print/blob/master/src/index.tsx#L328 --
Is there a way to set a more complex component as the trigger without wrapping in html elements, and if not, could you please update your docs to reflect what the trigger prop supports?

@MatthewHerbst
Copy link
Owner

MatthewHerbst commented May 15, 2020

Hi, thanks for the question.

You can use an arbitrarily complex component already, you just need to pass along the onClick that we provide in the cloned component. Something like:

const MyComponent = (props) => {
  const { onClick } = props; // `onClick` here is coming from `react-to-print`

  return <div onClick={onClick}>Hello World</div>;
}

const SomeComponentWithPrinting = () => {
  // NOTE: do not pas an `onClick` to `MyComponent` below, since `react-to-print` will override it
  return (
    <ReactToPrint
      ...
      trigger={() => <MyComponent />}
    />
  );
}

Does that work? I haven't tried it, but I feel like it should.

Regardless, we definitely should document this better. I will work on that in the next couple of days.

@BennettDixon
Copy link

BennettDixon commented May 15, 2020

Hello! I'm using this in a project and actually just came across this issue. Doing the following like @MatthewHerbst said works for me. I passed all props, but passing just the onClick probably works too although I didn't test that:

import React from 'react'

import * as S from './styles'

const PrintButton = props => <S.PrintIcon {...props} /> // a styled SVG icon

export default PrintButton

Then in the other file:

import React, { useRef } from 'react'
/* I have an index.js file in Components that imports and exports PrintButton as a named const, even though it is default in the last file shown. (I figured I would note this for anyone who is just copying and pasting) */
import { PrintButton } from 'app/components' 

const Test = (props) => {
    const printReference = useRef(null)

    return  (
        <>
            <p ref={printReference}>Print me</p>
            <ReactToPrint trigger={() => <PrintButton />} content={() => printReference.current} />
        </>
    )
}

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

No branches or pull requests

3 participants