npm模块copy-to-clipboard复制内容到剪切板


 

import React from 'react' import copy from 'copy-to-clipboard'

export default class App extends React.Component {
    constructor(props) {
        super(props)
        this.state = {
            content: ''
        }
    }

    render() {
        const { content } = this.state
        return (
            <div>
                <button onClick={this.copyToClipboard}>点击复制输入框内容</button>
                <input type='text' value={content} onChange={this.inputChangeHandler} ref={input => this.myInput = input} />
            </div>
        )
    }

    copyToClipboard = () => {
        // Get the value of the `value` attribute of the <Input> component
        // copy(this.myInput.value)
        copy(this.state.content)
    }

    inputChangeHandler = e => {
        console.log(`e.target.value=${e.target.value}`)
        console.log(`this.myInput.value=${this.myInput.value}`)
        this.setState({ content: e.target.value })
    }
}

---


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM