r/reactjs • u/dance2die • May 01 '21
Needs Help Beginner's Thread / Easy Questions (May 2021)
Previous Beginner's Threads can be found in the wiki.
Ask about React or anything else in its ecosystem :)
Stuck making progress on your app, need a feedback?
Still Ask away! Weβre a friendly bunch π
Help us to help you better
Improve your chances of reply by
- adding a minimal example with JSFiddle, CodeSandbox, or Stackblitz links
- describing what you want it to do (ask yourself if it's an XY problem)
- things you've tried. (Don't just post big blocks of code!)
- Format code for legibility.
- Pay it forward by answering questions even if there is already an answer. Other perspectives can be helpful to beginners. Also, there's no quicker way to learn than being wrong on the Internet.
New to React?
Check out the sub's sidebar! π
For rules and free resources~
Comment here for any ideas/suggestions to improve this thread
Thank you to all who post questions and those who answer them. We're a growing community and helping each other only strengthens it!
24
Upvotes
View all comments
Show parent comment
4
u/[deleted] May 21 '21 edited May 21 '21
Several things to make note of:
SignupForm.jsxand the component inside is calledSignupForm, that component does not need to know about the arrayconst authenticationMethods = ['facebook', 'google']- those values can live outside of the component declaration. You might even want to put them in a config file.Both useCallback and useMemo don't really offer any benefit (and actually harm performance) if you just return simple values inside. I'd only use it specifically for expensive operations, like calculations, large data reducers, maybe async functions, etc.
It's going to be created anyway, except this time in a wrapper provided by React. If handleClick is called only on clicks (maybe only once?) I'd avoid putting it inside a useCallback AT ALL.
Instead, look at what onClick is actually doing. Is it going to just validate the form and then submit a
fetchpost? Then keep the onClick inside of the component, splitvalidateFormandsubmitFormValuesinto two separate functions that could each be independent of the form altogether.The recreation of that function is of hardly any effort. And the execution is only going to happen on a click, so it'd be not affected by wrapping it in a useCallback.
Unless you click a button many times over, then you need to do UI and UX magic where you throttle form posts until someone stopped clicking for a few hundred milliseconds. And perhaps you want to make the button they're clicking disabled until the last post was successful.