How to convert jQuery code to React JS?
I have a button with text 'Copy'. When I click on it should change its text to 'Copied' and copy to Clipboard. Once copied, after few seconds I want the text to go back to 'Copy'. The following function will work I believe. I want incorporate this function into by React code.
$(function() {
var $btnCopy = $('#copy-button');
$btnCopy.on('click', function() {
var clipboard = new Clipboard('#copy-button');
clipboard.on('success', function(e) {
$btnCopy.text('Copied');
setTimeout(function() {
$btnCopy.text('Copy');
}, 2000);
});
});
});
class CopyButton extends Component {
constructor(props) {
super(props);
this.state = {
copyText: 'Copy'
};
}
copyToClipboard = () => {
this.setState({ copyText: 'Copied' });
const textField = document.createElement('textarea');
textField.innerText = JSON.stringify(this.props.content);
document.body.appendChild(textField);
textField.select();
document.execCommand('copy');
textField.remove();
};
render() {
return (
<Container>
<Header>
<Title>Testing</Title>
{this.props.content ? (
<Fragment>
<Button primary text="Copy" onClick={this.copyToClipboard}/>
<Button primary text="View" onClick={this.handleOpen} />
</Fragment>
</Header>
</Container>
export default CopyButton;