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

CSVLink is not forwarding ref to the original element #323

Open
kirillku opened this issue Apr 6, 2022 · 3 comments
Open

CSVLink is not forwarding ref to the original element #323

kirillku opened this issue Apr 6, 2022 · 3 comments

Comments

@kirillku
Copy link

kirillku commented Apr 6, 2022

After switching from material-ui@4 to mui@5 CSVLink stopped working for me because it is not forwarding ref to the original element.

Example:

import { Button } from "@mui/material";
import { CSVLink } from "react-csv";

<Button component={CSVLink} data={csvData}>
  Download me
</Button>

Codesandbox: https://codesandbox.io/s/mui-5-and-react-csv-we3q8f

Error:

TypeError
Cannot read properties of undefined (reading 'addEventListener')

What do you think about about rewriting CSVLink component as a functional component with usage of React.forwardRef? (I can submit a pr)

@kirillku
Copy link
Author

kirillku commented Apr 6, 2022

Workaround:

const LinkWithRef = React.forwardRef((props, ref) => (
  <CSVLink
    ref={(c) => {
      console.log("csvLink", c);
      console.log("ref", ref);
      ref(c?.link);
    }}
    {...props}
  />
));

<Button
  component={LinkWithRef}
  ref={(ref) => console.log(ref)}
  data={csvData}
>
  Download me
</Button>

Codesandbox: https://codesandbox.io/s/mui-5-and-react-csv-workaround-z33dbu

@leonbloy
Copy link

Same problem here after switching from material-ui@4 to mui@5

@JeomJeom
Copy link

@kirillku The workaround works well!!! Thank you

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