React.js

[1128] react CopyToClipboard - ํด๋ฆญ์‹œ ํ…์ŠคํŠธ ๋ณต์‚ฌํ•˜๊ธฐ

๋ฌธ์•ต 2021. 11. 29. 02:29

https://www.npmjs.com/package/react-copy-to-clipboard

 

react-copy-to-clipboard

Copy-to-clipboard React component

www.npmjs.com

 

๐Ÿš— react-copy-to-clipboard ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ด์šฉํ•ด์„œ ํด๋ฆญ์‹œ ํ…์ŠคํŠธ ๋ณต์‚ฌ ์ด๋ฒคํŠธ ๋งŒ๋“ค๊ธฐ

 

1. ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์„ค์น˜

$ npm install --save react react-copy-to-clipboard

 

 

2. ์‚ฌ์šฉ

import { CopyToClipboard } from "react-copy-to-clipboard";

 

<CopyToClipboard text={wallet} onCopy={()=>alert("์ฃผ์†Œ๊ฐ€ ๋ณต์‚ฌ๋˜์—ˆ์Šต๋‹ˆ๋‹ค")}>
  <div className="URL">{wallet}</div>
</CopyToClipboard>

 

์šฐ์„  import ํ•ด์™€์ค€๋‹ค.

 

<CopyToClipboard>  </CopyToClipboard> ์ด ์‚ฌ์ด์— ๋“ค์–ด๊ฐ€ ์žˆ๋Š” ์–˜๋“ค์„ ๋ˆ„๋ฅด๋ฉด ๋ณต์‚ฌ๊ฐ€ ๋˜๋Š”๊ฒƒ์ž„.

 

CopyToClipboard ์ปดํฌ๋„ŒํŠธ ์•ˆ์— text ์†์„ฑ์€ ๋‚ด๊ฐ€ ๋ณต์‚ฌํ•˜๊ณ  ์‹ถ์€ ํ…์ŠคํŠธ๋ฅผ ๋„ฃ์–ด์ค€๋‹ค.

์ผ๋ฐ˜ ํ…์ŠคํŠธ๋Š” " " ์•ˆ์— ๋„ฃ์–ด์ฃผ๋Š”๋ฐ, ๋‚˜๋Š” ์›ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ back์—์„œ ๊ฐ€์ ธ์™€์„œ ๊ฐ€๋ณ€์ ์œผ๋กœ ๋ฟŒ๋ ค์ฃผ๊ธฐ ์œ„ํ•ด์„œ { } ํ˜•ํƒœ๋กœ ์”€.

 

onCopy ์†์„ฑ๋„ ์žˆ์Œ. ์ด๊ฑด copy๋ฅผ ๊ฐ์ง€ํ•ด์„œ copy ๋์„๋•Œ ์ด๋ฒคํŠธ ๋„ฃ์„ ์ˆ˜ ์žˆ์Œ.

 

 

 

 

๋ฐ˜์‘ํ˜•