I want to copy text to the clipboard on clicking. I'm using "react-copy-to-clipboard" component. But it shows some error as the state is not defined and onCopy is not defined. Here's my code
import React from 'react';
import {GridList} from 'material-ui/GridList';
import Grid from '@material-ui/core/Grid';
import FontIcon from 'material-ui/FontIcon';
import {CopyToClipboard} from 'react-copy-to-clipboard';
import Typography from '@material-ui/core/Typography';
import gradientDownloader from './gradientDownloader';
function downloadGrad(){
console.log('It is clicked');
}
export default function GradientCard(props){
state = {
value: '',
copied: false,
};
onCopy = () => {
this.setState({copied: true});
};
return(
<div style={styles.root}>
<Grid container spacing={0} style={{maxWidth: '1360px', margin: '0 auto', paddingTop: '80px'}}>
<Grid item lg={9} md={10} sm={12} xs={12}>
<GridList
cellHeight='auto'
cols = {4}
padding= {16}
style={styles.gridList}
>
{
props.data.map(grad => {
return(
<div style={styles.card} key={grad.name}>
<div id="gradColor" style={{background: 'linear-gradient(to right, '+grad.colors[0]+', '+grad.colors[1]+'', height: '100px', borderRadius: '4px'}}></div>
<div>
<CopyToClipboard onCopy={this.onCopy} text={this.state.value}>
<span style={styles.hexValue} value={this.state.value}>{grad.colors[0]}</span>
</CopyToClipboard>
<FontIcon className="material-icons" style={styles.iconStyles}>arrow_right_alt</FontIcon>
<span style={styles.hexValue}>{grad.colors[1]}</span>
</div>
<Typography style={{paddingTop: '16px'}}>{grad.name}</Typography>
<span onClick={downloadGrad}>DOWNLOAD</span>
</div>
)
})
}
</GridList>
</Grid>
<Grid item lg={3} md={2} sm={12} xs={12}>
<Typography style={styles.gridList}>HELLO WORLD</Typography>
</Grid>
</Grid>
</div>
)
};
is the component i have used. I think that has to be rendered. if I had render before return it shows, unexpected token. Can anyone help me out with this.
GradientCard
is an Uncontrolled
component so you can't use state
in it. Make it a Controlled
component. Hope it works !!
You can try this way
npm install --save react react-copy-to-clipboard
import React from 'react';
import ReactDOM from 'react-dom';
import {CopyToClipboard} from 'react-copy-to-clipboard';
class App extends React.Component {
state = {
value: '',
copied: false,
};
render() {
return (
<div>
<input value={this.state.value}
onChange={({target: {value}}) => this.setState({value, copied: false})} />
<CopyToClipboard text={this.state.value}
onCopy={() => this.setState({copied: true})}>
<span>Copy to clipboard with span</span>
</CopyToClipboard>
<CopyToClipboard text={this.state.value}
onCopy={() => this.setState({copied: true})}>
<button>Copy to clipboard with button</button>
</CopyToClipboard>
{this.state.copied ? <span style={{color: 'red'}}>Copied.</span> : null}
</div>
);
}
}
const appRoot = document.createElement('div');
document.body.appendChild(appRoot);
ReactDOM.render(<App />, appRoot);
Please refer the below link https://www.npmjs.com/package/react-copy-to-clipboard
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.