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.

[Request] Expose `setValue` method from `useClipboard` hook

See original GitHub issue

Description

Please expose a new setValue method from the hook so users can update the value the onCopy method will copy to the user’s clipboard

Problem Statement/Justification

There are frequently times where a page has multiple components that can all be copied to a clipboard. The useClipboard hook doesn’t expose a way to change that value and, as a result, the dev is forced to rely on a re-render to execute for a useState value to update before the clipboard will work OR the dev will need to abstract the copy-able value into a new component so multiple hooks can exist on one page.

Proposed Solution or API

const defaultValue = 'some string';
const { onCopy, setValue } = useClipboard(defaultValue);

// ...

const updatedValue = 'some new string';
setValue(updatedValue);
onCopy();
// updatedValue copied to clipboard

Alternatives

Moving all relevant copy-able elements to their own components (ideal from an abstraction standpoint, but often unnecessary if the original file is trivially small)

Additional Information

No response

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
nachoiacovinocommented, Nov 16, 2022

I ended up doing this to fix it on every part of the app we use useClipboard:

const { onCopy, setValue } = useClipboard(address || "");

useEffect(() => {
  if (address) {
    setValue(address);
  }
}, [address, setValue]);

Leaving this for future reference but seems a bit redundant having to always do this.

2reactions
RobinClowerscommented, Nov 16, 2022

This is an extremely confusing api for a hook. If a hook takes a value, it should react to changes in that value, not require calling a separate function to update it. This was also a breaking change despite what https://github.com/chakra-ui/chakra-ui/pull/6763 says.

Read more comments on GitHub >

github_iconTop Results From Across the Web

useForm - setValue - React Hook Form
This function allows you to dynamically set the value of a registered field and have the options to validate and update the form...
Read more >
useSlider – React Aria - React Spectrum Libraries
Documentation for useSlider in the React Aria package.
Read more >
useControllableState - Chakra UI
The useControllableState hook returns the state and function that updates the state, ... const [value, setValue] = useControllableState(options).
Read more >
Macro Express 5 Manual of Instruction
If you want to have the macro paste the text into your application via the. Windows clipboard, select the Use Clipboard to Paste...
Read more >
https://opensource.apple.com/source/WebKit/WebKit-...
WebCoreSupport/ChromeClientGtk.cpp: Use the new helper method from GtkUtilities. ... (webkit_hit_test_result_init): call C++ ctors in private data.
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