0

I have a Flask back-end and a React front-end. At some point data is sent from back-end to front-end where the data is used for several things. Either way, when the data comes from the back-end to the front-end I just save it as follows when the page loads:

const [data, setData] = useState[]

useEffect(() => {
    setData(<data from backend>);
}, []);

Hence, my data should now be stored as data. And data here is just an array of numbers/values. Now I would like to just click a button or something, and then copy the data to the clipboard, where I can then paste it into Excel or something.

I just can't seem to find any good solution to this. So I was wondering how this is done, and if it is even possible without writing 100 lines of code ?

2
  • 1
    See Clipboard#writeText: navigator.clipboard.writeText(data.join('\n')) - And in case you have multiple columns, you can first join the columns with \t and then the rows with \n, then it will also work nicely when pasted in Excel. Commented May 30, 2022 at 14:45
  • Does this answer your question? How do I copy to the clipboard in JavaScript? Commented May 30, 2022 at 14:46

1 Answer 1

1

You can set the clipboard text with :

navigator.clipboard.writeText(data)

and you can set onClick event or set that in defined useEffact hook after fetching data.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.