react-copy-to-clipboard
react-copy-to-clipboard copied to clipboard
Multiple CopyToClipboard on the same page don't work
Hi,
I have two <CopyToClipboard> components on one page. If I click on one the first time, the text gets copied. When I click on the second button or click again on the first button I get an undefined
text.
<h3>Embed code</h3>
<div className={`${styles.codeBox} p-4 d-flex flex-column mb-5 position-relative`}>
<iframe src="https://stikii.co/campaigns/5aday/" style="border:0px #ffffff none;" name="myiFrame" scrolling="no" frameborder="1" marginheight="0px" marginwidth="0px" height="400px" width="600px" allowfullscreen></iframe>
<div className="d-flex flex-column flex-lg-row justify-content-between align-items-end">
<span className={styles.copiedMessage}>{copy.copied ? "Copied!" : null}</span>
<CopyToClipboard text={copy.value}
onCopy={(text, result) => {
console.log(text);
console.log(copy.value);
setCopy({copied: true});
setCopyUrl({copied: false});
}}>
<span className='mainButton align-self-end mt-4'>Copy code</span>
</CopyToClipboard>
</div>
</div>
<h3>Link to Stikii</h3>
<div className={`${styles.codeBox} p-4 d-flex flex-column position-relative`}>
https://stikii.co/campaigns/5aday/
<div className="d-flex flex-column flex-lg-row justify-content-between align-items-end">
<span className={styles.copiedMessage}>{copyUrl.copied ? "Copied!" : null}</span>
<CopyToClipboard text={copyUrl.value}
onCopy={(text, result) => {
console.log(text);
console.log(copyUrl.value);
setCopyUrl({copied: true});
setCopy({copied: false});
}}>
<span className='mainButton align-self-end mt-4'>Copy URL</span>
</CopyToClipboard>
</div>
</div>
Is it not meant to work with two or more copy buttons?
Thanks
The problem with implementation. You can put as many copy buttons on the page as necessary they don't clash. I cannot suggest any fixes for your code without codepen/sandbox example
Sure, here is the sandbox https://codesandbox.io/embed/competent-worker-ultdrh?fontsize=14&hidenavigation=1&theme=dark
Thanks for your help
Your problem is unrelated. You are updating your state and removing the value. Use this
setState(s => ({...s, copied: true | false}))
Doing so, you are not removing the other props from your state.