【问题标题】:e.preventDefault() not a function NextJS/Reacte.preventDefault() 不是 NextJS/React 函数
【发布时间】:2020-11-05 11:20:57
【问题描述】:

我发现的唯一其他答案是事件没有传递给函数。这里情况不同。我显然将事件作为变量 e 传递。我将按钮类型设置为提交,因此表单正在与事件一起提交。此外,handleChange 函数与事件一起正常工作。我从我正在学习的 Udemy 课程中复制了这种格式,我几乎拥有完全相同的东西。我能想到的唯一大区别是,Udemy 课程将所有这些逻辑都放在一个单独的组件中,该组件被导入然后呈现到页面,其中表单和表单处理逻辑是此页面上的主要组件。

请查看我的代码并告诉我我做错了什么!

错误:

代码:

import React, { useState, useEffect } from 'react'
import Router, { withRouter } from 'next/router';
import Layout from '../../components/Layout';
import { Col, Row, Button, Form, FormGroup, Label, Input } from 'reactstrap';
import { createPatient } from '../../actions/patient';

const CreatePatient = ({ router }) => {

    const [ values, setValues ] = useState({
        error: '',
        formData: '',
        data: ''
    });

    const { error, formData } = values;

    useEffect(() => {
        setValues({
            ...values,
            formData: new FormData()
        })
    }, [router])

    const createPatient = (e) => {
        e.preventDefault();
        
        createPatient(formData).then(data => {
            if (data.error) {
                setValues({ ...values, error: data.error });
            }
        })
    }

    const handleChange = name => e => {
        const value = e.target.value;
        
        formData.set(name, value);

        setValues({ ...values, [name]: value, formData, error: '' });
    }

    return (
        <Layout>
            <section className="container pt-3">
                <form className="form" onSubmit={ createPatient }>
                    <div className="row">
                        <div className="col-md-4 my-2">
                            <label htmlFor="firstName">First Name</label>
                            <input type="text" className="form-control" name="firstName" id="firstName" onChange={handleChange('firstName')}/>
                        </div>
                        <div className="col-md-4 my-2">
                            <label htmlFor="lastName">Last Name</label>
                            <input type="text" className="form-control" name="lastName" id="lastName" onChange={handleChange('lastName')}/>
                        </div>
                    </div>
                    <div className="row">
                        <div className="col-sm-2">
                            <button type="submit" className="btn btn-primary">Create</button>
                        </div>
                    </div>
                </form>
            </section>
        </Layout>
    )
}

export default withRouter(CreatePatient);

【问题讨论】:

  • 你正在覆盖createPatient

标签: node.js reactjs next.js


【解决方案1】:

您在createPatient 内部调用createPatient,同时传递了一个非事件。而不是这个:

const createPatient = (e) => {
    e.preventDefault();
        
    createPatient(formData).then(data => {
        if (data.error) {
            setValues({ ...values, error: data.error });
        }
    })
}

只需将其更改为更像这样的内容:

const createPatient = (e) => {
    e.preventDefault();
        
    // run your API call here, then...
    if (data.error) {
        setValues({ ...values, error: data.error });
    }
}

【讨论】:

  • 不敢相信我没有抓住这个!!所以第一次调用实际上是正确的,但我再次递归调用它,'e'第二次是 formData。我现在明白了(愚蠢的错误哈哈)
【解决方案2】:

createPatient 方法中的 e 符号不是事件,这就是您收到错误的原因。在提交中,您会得到返回的元素,而不是事件。所以,e 这是你的元素。

标记

<form className="form" onSubmit={ createPatient }

js

const createPatient = (e) => {
    console.log(e); // the form div
    
    createPatient(formData).then(data => {
        if (data.error) {
            setValues({ ...values, error: data.error });
        }
    })
}

【讨论】:

  • 有道理。我如何获得活动?
  • NeoNexus Demortis 的回答实际上是正确的。我两次使用名称 createPatient,再次调用该函数。传递的“e”实际上是事件,我只是在 createPatient 函数中递归调用 createPatient,而第二次我没有传递事件,而是传递了 formData。我接受了他的回答。
猜你喜欢
  • 1970-01-01
  • 2022-07-20
  • 2023-01-14
  • 2019-05-07
  • 2021-05-30
  • 1970-01-01
  • 1970-01-01
  • 2019-10-26
  • 2021-08-04
相关资源
最近更新 更多