-
-
Notifications
You must be signed in to change notification settings - Fork 222
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
Trigger does nothing if it returns a React component #248
Comments
Hi, thanks for the question. You can use an arbitrarily complex component already, you just need to pass along the 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. |
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} />
</>
)
} |
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?
The text was updated successfully, but these errors were encountered: