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.

useInitialValue util

See original GitHub issue

I’m trying to learn jotai, it’s great. I’m trying to write a custom hook that will only get the initial value and not re-render on consquent updates. I tried this which obviously doesn’t work. Any tips on how I can accomplish this? Just a learning thing.

function useInitialValueAtom(atom) {
  const [value] = useAtom(atom);
  const savedValue = useRef(value);
  return savedValue.current;
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
Aslemammadcommented, Jan 13, 2021

@Noitidart I don’t think so, I loved your idea + @merisbahti solution, @dai-shi What’s your opinion about adding something like this to the utils?

1reaction
dai-shicommented, Jan 14, 2021

@Noitidart Thanks for the use case. RHF is nice for uncontrolled forms.

So, conceptually, the default value and the current value have different values. How about something like this?

const defaultTextAtom = atom("hello")

const currentTextAtom = atom("hello")

I think having atom values in ref out of sync is not React-ish, and better to avoid it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

rrRoadRunner — libRoadRunner 1.1.16 documentation
useInitialValue – a boolean value to indicate whether using initial value as current value when the assignment rule of a variable is removed...
Read more >
initialValues - React Advanced Form - GitBook
An object that describes initial values of the fields. Definition.
Read more >
ariakit-utils v0.17.0-next.26 Bundlephobia
Size of ariakit-utils v0.17.0-next.26 is 18.5 kB (minified), and 5.7 kB when compressed using GZIP. Bundlephobia helps you find the performance impact of ......
Read more >
Auto Complete Text Box - TechDocs
Always Use Initial Value. Sets whether the initial value is always displayed upon first entering the form. Escape HTML. Sets whether the component...
Read more >
@angular/common | Yarn - Package Manager
72092ebd26, feat, Allow a FormControl to use initial value as default. ... /packages/compiler/src/render3/view/util.ts#L174-L206; language-service: resolve ...
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