handleSubmit is not defined? what does that mean? whenever I execute white screen is shown. please solve the problem

huangapple go评论73阅读模式
英文:

handleSubmit is not defined? what does that mean? whenever I execute white screen is shown. please solve the problem

问题

The code you provided contains JavaScript and JSX, and it seems you're encountering an issue related to the "handleSubmit is not defined" error. This error occurs because the handleSubmit function is defined within the FormData function, but you are trying to use it outside of that scope. To fix this issue, you should define handleSubmit at the outermost level so that it's accessible within the return block. Here's the corrected code:

function Data() {
    const [fname, setFname] = useState('');
    const [lname, setLname] = useState('');

    const handleSubmit = async (e) => {
        e.preventDefault();

        try {
            const response = await axios.post('/server', {
                fname,
                lname,
            });
            console.log(response.data)
        } catch (error) {
            console.error(error);
        }
    }

    return (
        <div>
            <form onSubmit={handleSubmit}>
                <label>
                    First Name:
                    <input type="text" value={fname} onChange={(e) => setFname(e.target.value)}></input>
                </label>
                <label>
                    Last Name:
                    <input type="text" value={lname} onChange={(e) => setLname(e.target.value)}></input>
                </label>
                <button type='submit'>Submit</button>
            </form>
        </div>
    )
}

This code defines handleSubmit at the same level as useState hooks, making it accessible for the form's onSubmit handler.

英文:

handleSubmit is not defined? what does that mean? whenever I execute white screen is shown. please solve the problem. handleSubmit is not defined? what does that mean? whenever I execute white screen is shown. please solve the problem. handleSubmit is not defined? what does that mean? whenever I execute white screen is shown. please solve the problem.

function Data() {

FormData = () =&gt; {
    const [fname, setFname] = useState(&#39;&#39;);
    const [lname, setLname] = useState(&#39;&#39;);

    const handleSubmit = async (e) =&gt; {
        e.preventDefault();

        try {
            const response = await axios.post(&#39;/server&#39;, {
                fname,
                lname,
            });
            console.log(response.data)
        } catch (error) {
            console.error(error);
        }
    }
}

return (

    &lt; div &gt;
        &lt;form onSubmit={handleSubmit}&gt;
            &lt;label&gt;
                First Name:
                &lt;input type=&quot;text&quot; value={this.FormData.fname} onChange={(e) =&gt; this.FormData.setFname(e.target.value)}&gt;&lt;/input&gt;
            &lt;/label&gt;
            &lt;label&gt;
                Last Name:
                &lt;input type=&quot;text&quot; value={this.FormData.lname} onChange={(e) =&gt; this.FormData.setLname(e.target.value)}&gt;&lt;/input&gt;
            &lt;/label&gt;
            &lt;button type=&#39;submit&#39;&gt;Submit&lt;/button&gt;
        &lt;/form&gt;
    &lt;/div &gt;

)

}

答案1

得分: 3

你在无法访问return语句的地方添加了handleSubmit。请使用以下代码:

const Data = () => {
    const [fname, setFname] = useState('');
    const [lname, setLname] = useState('');

    const handleSubmit = async (e) => {
        e.preventDefault();

        try {
            const response = await axios.post('/server', {
                fname,
                lname,
            });
            console.log(response.data);
        } catch (error) {
            console.error(error);
        }
    }

    return (
        <div>
            <form onSubmit={handleSubmit}>
                <label>
                    First Name:
                    <input type="text" value={fname} onChange={(e) => setFname(e.target.value)}></input>
                </label>
                <label>
                    Last Name:
                    <input type="text" value={lname} onChange={(e) => setLname(e.target.value)}></input>
                </label>
                <button type='submit'>Submit</button>
            </form>
        </div>
    )
}
英文:

You've added handleSubmit in a place where the return statement cannot access it. Please use this:

const Data = () =&gt; {
    const [fname, setFname] = useState(&#39;&#39;);
    const [lname, setLname] = useState(&#39;&#39;);

    const handleSubmit = async (e) =&gt; {
        e.preventDefault();

        try {
            const response = await axios.post(&#39;/server&#39;, {
                fname,
                lname,
            });
            console.log(response.data)
        } catch (error) {
            console.error(error);
        }
    }

    return (

        &lt; div &gt;
            &lt;form onSubmit={handleSubmit}&gt;
                &lt;label&gt;
                    First Name:
                    &lt;input type=&quot;text&quot; value={this.FormData.fname} onChange={(e) =&gt; this.FormData.setFname(e.target.value)}&gt;&lt;/input&gt;
                &lt;/label&gt;
                &lt;label&gt;
                    Last Name:
                    &lt;input type=&quot;text&quot; value={this.FormData.lname} onChange={(e) =&gt; this.FormData.setLname(e.target.value)}&gt;&lt;/input&gt;
                &lt;/label&gt;
                &lt;button type=&#39;submit&#39;&gt;Submit&lt;/button&gt;
            &lt;/form&gt;
        &lt;/div &gt;
    
    )
}

huangapple
  • 本文由 发表于 2023年3月7日 19:13:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75661248.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定