Reputation: 83
I have 2 questions ...
The loading component is not rendering:
const Register: FC<RouteComponentProps> = () => {
const [loading, setLoading] = useState<boolean>(false)
const [success, setSuccess] = useState<boolean>(false)
...
const onSubmit = async(values: Values) => {
setLoading(true)
try {
const response = await register({
variables: { data: { firstName: values.first_name, lastName: values.last_name, email: values.user_email, password: values.password } }
})
setLoading(false)
if (response && response.data && response.data.register) {
setSuccess(true)
}
} catch (error) {
setLoading(false)
...
}
}
return (
...
{ loading && <Loading/> }
)
Where am I wrong, kindly let me know.
Thank you!
Upvotes: 4
Views: 4832
Reputation: 2203
Assuming you have a way to know whether your Task result is pending, you can use the useThrottledValue hook from mantine or the useThrottle hook from use-hooks. Unlike most solutions to this problem, this solution has two crucial differences.
const { loading: accountLoading, ... } = YOUR_OBJECT;
const accountLoadingDelayed = useThrottledValue(accountLoading, accountLoading ? 400 : 0);
Upvotes: 0
Reputation: 31
you can use with Promise.all. define your fetch and define delay with promise, and you should wait when the fetch and minimum delay will finish. in general its will be like that:
function delayPromise(ms: number) {
return new Promise(resolve => setTimeout(resolve, ms));
}
const requestPromise= fetch(....);
const res = await Promise.all([requestPromise, delayPromise(1000)]);
and in your code I think you need change to:
const registerPromise = register({
variables: { data: { firstName: values.first_name, lastName: values.last_name, email: values.user_email, password: values.password } }
});
const res = await Promise.all([registerPromise, delayPromise(1000)]); // your register response found in res[0]
Upvotes: 1
Reputation: 1985
To show the spinner for at least 1 second or the actual request time if longer than 1 second:
const onSubmit = async(values: Values) => {
setLoading(true)
try {
const now = new Date().valueOf()
const response = await register(...)
const waitTime = Math.max(0, -(new Date().valueOf() - now) + 1000)
await new Promise(resolve => setTimeout(resolve, waitTime))
} catch (error) {
// handle error
} finally {
setLoading(false)
}
}
Upvotes: 1
Reputation: 577
You dont need to show loading to min 1 second since loading icon is already shown when fetching data and in real life it will be more than 1 sec.
if u want to set min 1 sec anyways
try {
const response = await register({
variables: { data: { firstName: values.first_name, lastName: values.last_name, email: values.user_email, password: values.password } }
})
//change here
setTimeout(() => setLoading(false), 1000);
Upvotes: 3