0

I'm wondering why the event.preventDefault isn't working in my post request. When I click the SAVE SETTINGS button, page reloads. If I just put a console.log after my event.preventDefault() it works just fine, so the problem is in my request I suppose. Here's the code, any help is appreciated.

import React, { useEffect, useState } from 'react'
import Button from '../shared/formElements/Button';
import { useHttpClient } from '../shared/hooks/http-hook';
import { useForm } from '../shared/hooks/form-hook';
    ...
    ...

    const Settings = () => {
        const { isLoading, error, sendRequest, clearError } = useHttpClient();
        const [loadedSettings, setLoadedSettings] = useState();
        const [formState, inputHandler] = useForm(
        {
            hostname:
            {
                value: '',
                isValid: false
            },
            username:
            {
                value: '',
                isValid: false
            },
            password:
            {
                value: '',
                isValid: false
            },
    
    ...
    ...
            const settingsSubmitHandler = async event => {
                    event.preventDefault()
                    try {
                        await sendRequest(
                            'http://localhost/api/settings/save',
                            'POST',
                            JSON.stringify({
                                hostname: formState.inputs.hostname.value,
                                username: formState.inputs.username.value,
                                password: formState.inputs.password.value,
                                ...
            
                                ...
                        }),
                        {
                            'Content-Type': 'application/json'
                        }
                    );
                } catch (err) { }
            };

And this my JSX code:

    return (
            <React.Fragment>
                <ErrorModal error={error} onClear={clearError} />
                {isLoading && <LoadingSpinner asOverlay />}
                {!isLoading && loadedSettings && (
                    <form className="settings-form" onSubmit={settingsSubmitHandler}>
                        <div className="container">

            ...

            ...

            <div className="col" align="center">
                <Button type="submit" disabled={!formState.isValid}>SAVE SETTINGS</Button>
            </div>

            ...
3
  • Could you try removing your catch, your swallowing any exceptions that might give you a clue.. Commented Jul 12, 2020 at 14:19
  • @FDG what do you mean by "it reloads my component"? does the whole page gets reloaded? Commented Jul 12, 2020 at 15:42
  • @Apostolos Yes it does Commented Jul 12, 2020 at 21:29

2 Answers 2

2

Probably, it's because you use async function and the function body is called in the next tick. If you remove async from function definition, it should work:

const settingsSubmitHandler = event => {
  event.preventDefault()
  sendRequest(...).then(()=> {
    // some async code here
  })
}
Sign up to request clarification or add additional context in comments.

3 Comments

That won't stop preventDefault
Hi Ramil, thanks for your answer, but this unfortunately doesn't work
Really, @Keith is right, async function doesn't block preventDefault. Looks like the problem is not in presented code.
0

It seems the problem was {!isLoading && loadedSettings && (, just removing !isLoading now it works fine.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.