question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Usage with React (just an example)

See original GitHub issue

Description

First of all, thanks and congrats for clipboard.js, it is a big enhancement for every web developer.

I am using clipboard.js with React and it works. I would like to add style from material-ui.

At first I was using the data-clipboard-target attribute, then I noticed the last part of documentation that explaing how to set target dynamically.

So I solved my issue while I was opening it, just reading the docs. I just want to report my working example, to give back to the community.

Minimal example

import React from 'react'

class ResultURL extends React.Component {
  componentDidMount () {
    const {
      buttonId,
      inputId
    } = this.props

    const buttonIdSelector = `#${buttonId}`

    const Clipboard = this.props.clipboard

    this.clipboard = new Clipboard(
      buttonIdSelector, {
        target: () => document.getElementById(inputId)
      }
    )
  }

  render () {
    const {
      buttonId,
      inputId,
      value
    } = this.props

    const inputIdSelector = `#${inputId}`

    return (
      <div>
        <input
          id={inputId}
          type={'text'}
          value={value}
          readOnly
        />
        <button
          id={buttonId}
        > Copy
        </button>
      </div>
    )
  }
}

ResultURL.propTypes = {
  clipboard: React.PropTypes.func.isRequired,
  value: React.PropTypes.string,
  inputId: React.PropTypes.string.isRequired,
  buttonId: React.PropTypes.string.isRequired
}

export default ResultURL

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:7
  • Comments:12 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
zenorochacommented, Jun 8, 2016

Done: https://github.com/zenorocha/clipboard.js/wiki/Guides 😉

Thanks @fibo for sharing your experience!

2reactions
fibocommented, Jun 7, 2018

@Clee681 Yes it makes more sense, codepen updated, 10x

Read more comments on GitHub >

github_iconTop Results From Across the Web

Hello World - React
It displays a heading saying “Hello, world!” on the page. Try it on CodePen. Click the link above to open an online editor....
Read more >
What is React.js? (Uses, Examples, & More) - HubSpot Blog
A single-page application loads only a single HTML document on the first request. Then, it updates the specific portion, content, or body of ......
Read more >
What And Why React.js - C# Corner
ReactJS is just simpler to grasp right away. The component-based approach, well-defined lifecycle, and use of just plain JavaScript make React ...
Read more >
10 Famous React Apps: Examples (2022) - Brainhub
Read why these super-popular React apps use the technology: Facebook, Instagram, Netflix, Whatsapp, The New York Times, Yahoo Mail, Khan Academy, and more....
Read more >
React Router DOM: How to handle routing in web apps
Only in some special cases you'll have to use <Router> directly (for example when working with Redux), so the first thing you have...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found