英文:
Using Chakra UI's useClipboard hook
问题
I have multiple input fields populated with data gotten from redux store. I need help to implement a copy to clipboard function for each input field. Tried doing this but no luck:
import { useClipboard } from "@chakra-ui/react";
const { sandboxKey, token } = useSelector((state) => state.apikeys);
const { onCopy, value, setValue, hasCopied } = useClipboard("");
return (
<InputGroup>
<Input
value={token}
onChange={(e) => {
setValue(e.target.value);
}}
/>
<InputRightElement>
<Button onClick={onCopy}>
{hasCopied ? "Copied!" : "Copy"}
</Button>
</InputRightElement>
</InputGroup>
<InputGroup>
<Input
value={prodkey.prodKey}
onChange={(e) => {
setValue(e.target.value);
}}
/>
<InputRightElement>
<Button onClick={onCopy}>
{hasCopied ? "Copied!" : "Copy"}
</Button>
</InputRightElement>
</InputGroup>
)
Note: I've provided the translated JavaScript code as requested.
英文:
I have multiple input fields populated with data gotten from redux store. I need help to implement a copy to clipboard function for each input field. Tried doing this but no luck:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
import { useClipboard } from "@chakra-ui/react";
const { sandboxKey, token } = useSelector((state) => state.apikeys);
const { onCopy, value, setValue, hasCopied } = useClipboard("");
return (
<InputGroup>
<Input
value={token}
onChange={(e) => {
setValue(e.target.value);
}}
/>
<InputRightElement>
<Button onClick={onCopy}>
{hasCopied ? "Copied!" : "Copy"}
</Button>
</InputRightElement>
</InputGroup>
<InputGroup>
<Input
value={prodkey.prodKey}
onChange={(e) => {
setValue(e.target.value);
}}
/>
<InputRightElement>
<Button onClick={onCopy}>
{hasCopied ? "Copied!" : "Copy"}
</Button>
</InputRightElement>
</InputGroup>
)
<!-- end snippet -->
答案1
得分: 0
你应该调用useClipboard
钩子两次,分别从两个不同的输入中复制。此外,你应该将钩子返回的值传递给Input的value,而不是始终传递相同的值。
const tokenCopy = useClipboard(token);
const prodKeyCopy = useClipboard(prodkey.prodKey);
return (
<>
<InputGroup>
<Input
value={tokenCopy.value}
onChange={e => tokenCopy.setValue(e.target.value)}
/>
<InputRightElement>
<Button onClick={tokenCopy.onCopy}>
{tokenCopy.hasCopied ? "已复制!" : "复制"}
</Button>
</InputRightElement>
</InputGroup>
<InputGroup>
<Input
value={prodKeyCopy.value}
onChange={e => prodKeyCopy.setValue(e.target.value)}
/>
<InputRightElement>
<Button onClick={prodKeyCopy.onCopy}>
{prodKeyCopy.hasCopied ? "已复制!" : "复制"}
</Button>
</InputRightElement>
</InputGroup>
</>
);
英文:
You should call the useClipboard
hook twice for copying from two distinct inputs. In addition, you should pass the value returned by the hook to the Input's value instead of always passing the same value.
const tokenCopy = useClipboard(token);
const prodKeyCopy = useClipboard(prodkey.prodKey);
return (
<>
<InputGroup>
<Input
value={tokenCopy.value}
onChange={e => tokenCopy.setValue(e.target.value)}
/>
<InputRightElement>
<Button onClick={tokenCopy.onCopy}>
{tokenCopy.hasCopied ? "Copied!" : "Copy"}
</Button>
</InputRightElement>
</InputGroup>
<InputGroup>
<Input
value={prodKeyCopy.value}
onChange={e => prodKeyCopy.setValue(e.target.value)}
/>
<InputRightElement>
<Button onClick={prodKeyCopy.onCopy}>
{prodKeyCopy.hasCopied ? "Copied!" : "Copy"}
</Button>
</InputRightElement>
</InputGroup>
</>
);
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论