将文本复制到剪贴板react.js

时间:2018-05-22 07:14:54

标签: reactjs

我想在点击时将文本复制到剪贴板。我正在使用“react-copy-to-clipboard”组件。但它显示了一些错误,因为未定义状态且未定义onCopy。这是我的代码

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>
    )
};

是我使用的组件。我认为必须提出。如果我在返回之前有渲染它显示,意外的令牌。任何人都可以帮我解决这个问题。

2 个答案:

答案 0 :(得分:0)

GradientCardUncontrolled组件,因此您无法在其中使用state。将其设为Controlled组件。希望它有效!!

答案 1 :(得分:0)

你可以试试这种方式

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);

请参阅以下链接  https://www.npmjs.com/package/react-copy-to-clipboard