import "./App.css";
import {useRef} from 'react';
function App() {
const myInput=useRef(null);
function handleClickCopy() {
const copyText=myInput.current.value;
if(copyText){
const copyInput=document.createElement('input');
copyInput.setAttribute('value', copyText);
document.body.appendChild(copyInput);
copyInput.select();
try{
document.execCommand('copy');
document.body.removeChild(copyInput);
console.log('复制成功');
}catch(err){
console.log(err, '浏览器不兼容');
document.body.removeChild(copyInput);
}
}else{
console.log('请输入内容');
}
}
return (
<div className="App">
<header className="App-header">
<input type="text" ref={node=>{myInput.current=node;}}/>
<button onClick={handleClickCopy}>点击复制</button>
</header>
</div>
);
}
export default App;