1

I'm trying to use react-clipboard inside a react component to allow a user to easily copy-paste some text into clipboard but my code isn't working. I wonder if I'm missing something obvious. Here is my code (pls ignore the boilerplate for closing the modal):

'use strict';
    import React from 'react';
    import { Modal } from 'react-bootstrap';
    import Clipboard from "react-clipboard";

    class CopyText extends React.Component {
      render() {
        let text = JSON.stringify(this.props.value, null, "  ");

        return (
          <div>
            <p>Press Cmd + C to copy:</p>
            <pre>{text}</pre>
            <Clipboard value={text} />
          </div>
          );    
      }
    }

    class CopyLinkModal extends React.Component {
      constructor(props) {
        super(props)
        this.onClick = this.onClick.bind(this);
      }
      onClick() {
        this.props.onHide();
      }
      render() {
        return (
          <Modal show={this.props.show} message={this.props.message} onHide={this.onClick}>
            <Modal.Body>
              <div className="linkMessage">
                <CopyText value={this.props.message} />
              </div>
            </Modal.Body>
            <Modal.Footer>
              <button onClick={this.onClick}>Close</button>
            </Modal.Footer>
          </Modal>
        )
      }
    }

    export default CopyLinkModal;

For the most part I'm following instruction from https://www.npmjs.com/package/react-clipboard but I'm still getting the TypeError: TypeError: Can't add property context, object is not extensible Thanks for any help.

7
  • Where do you get this error (line number, stack trace)? When do you get the error (compiling, executing, user action)? Commented Aug 26, 2015 at 23:21
  • Bergi - the error originates from ReactCompositeComponent.js:153 - I dont see any of my own files in the call stack. There is also an warning that I think is related: (Warning: Component(...): No render method found on the returned component instance: you may have forgotten to define render in your component or you may have accidentally tried to render an element whose type is a function that isn't a React component.) The error is triggered when a user clicks a button which itself triggers the modal to be displayed (parent component state is changed to true). Commented Aug 26, 2015 at 23:41
  • Hm, did you check that all your imports work as expected? Commented Aug 26, 2015 at 23:48
  • Clipboard is the only module I added recently... App works fine without it. There is an outside chance that its buggy or there some issue with ES6 syntax. Commented Aug 27, 2015 at 0:13
  • 1
    I see you're using ES6 Classes. My guess is that the React version used by react-clipboard precedes this feature. Have a go using React.createClass in your component. Commented Aug 27, 2015 at 7:30

1 Answer 1

2

From react-clipboard package.json:

"dependencies": {
    "react": "^0.12.2"
}

Therefore ES6 classes are not supported.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.