React 單擊進行復制
阿新 • • 發佈:2018-11-28
安裝
npm install --save react react-copy-to-clipboard
使用
const App = React.createClass({ getInitialState() { return {value: '', copied: false}; }, onChange({target: {value}}) { this.setState({value, copied: false}); }, onCopy() { this.setState({copied: true}); }, render() { return ( <div> <h1>CopyToClipboard</h1> <input value={this.state.value} size={10} onChange={this.onChange} /> <CopyToClipboard text={this.state.value} onCopy={this.onCopy}> <span>Copy to clipboard with span</span> </CopyToClipboard> <CopyToClipboard text={this.state.value} onCopy={this.onCopy}> <button>Copy to clipboard with button</button> </CopyToClipboard> {this.state.copied ? <span style={{color: 'red'}}>Copied.</span> : null} <br /> <textarea style={{marginTop: '1em'}} cols="22" rows="3" /> </div> ); } }); const appRoot = document.createElement('div'); appRoot.id = 'app'; document.body.appendChild(appRoot); ReactDOM.render(<App />, appRoot);