[a / b / c / d / e / f / g / gif / h / hr / k / m / o / p / r / s / t / u / v / vg / vm / vmg / vr / vrpg / vst / w / wg] [i / ic] [r9k / s4s / vip / qa] [cm / hm / lgbt / y] [3 / aco / adv / an / bant / biz / cgl / ck / co / diy / fa / fit / gd / hc / his / int / jp / lit / mlp / mu / n / news / out / po / pol / pw / qst / sci / soc / sp / tg / toy / trv / tv / vp / vt / wsg / wsr / x / xs] [Settings] [Search] [Mobile] [Home]
Board
Settings Mobile Home
/wsr/ - Worksafe Requests


Thread archived.
You cannot reply anymore.


[Advertise on 4chan]


File: submission_handler.png (64 KB, 671x455)
64 KB
64 KB PNG
Hey, I was wondering if anyone could offer some suggestions regarding an issue I'm having with a React app. I'm taking an online course, and they've tasked me with making a mock "portfolio" website. They've provided most of the code, I just primarily have to configure stuff correctly.
The first time I click "submit," nothing happens; this is because the response object is still null, which it is initialized to using the useState hook. useSubmit is a custom hook which wraps a "submit" helper function (see my next post with the screenshot). Even though I made my onSubmit asynchronous (pic related), and force the app to wait for it to complete before opening an alert using onOpen, the app still doesn't wait for the useSubmit hook to return the response object's updated values before submitting the form.
From what I understand, hooks themselves cannot be asynchronous, but they can wrap asynchronous operations. So my idea is that onSubmit waits for the submit helper to complete, but doesn't wait for the hook to return the results. After doing some console logging, I have noticed that upon subsequent form submissions, the form submits the PREVIOUS submission attempt's data, which makes sense because upon clicking "submit," the submission event occurs before the NEW submission data is handled by the useSubmit hook. However, the old submission's data now populates the response object.
Pic: the useSubmit hook (top) and submission handler (onSubmit)
Next pic will be the implementation of useSubmit
>>
File: useSubmit_hook.png (74 KB, 804x591)
74 KB
74 KB PNG
Here is the useSubmit hook's implementation. The hook simulates an API call; it waits for 2 seconds ( const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); ) then sets a response, based on a random number; there's a ~50% chance of success or failure.
Any ideas how I can get the hook to return the data ( return { isLoading, response, submit }; ) before onOpen tries to open the alert? Or is the implementation they supplied me with just flawed? I know it probably doesn't matter much for this little project, but it kinda bugs me.
>>
bamp



[Advertise on 4chan]

Delete Post: [File Only] Style:
[Disable Mobile View / Use Desktop Site]

[Enable Mobile View / Use Mobile Site]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.