e.preventDefault is not a function (React) 不要翻译。

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

e.preventDefault is not a function (React)

问题

I am using React to make a form that will make a POST to MongoDB, what happens is that when I start trying to fill the form I get the following error:

e.preventDefault is not a function

TypeError: e.preventDefault is not a function
at handleChange

this is the code (in a summarized form), this is handleChange and handleSubmit:

 const handleChange = (e) => {
    e.preventDefault();
    setProject({ ...project, [e.target.name]: e.target.value });
  };

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

    const res = await fetch("http://localhost:5001/client/projects", {
      method: "POST",
      body: JSON.stringify(project),
      headers: { "Content-Type": "application/json" },
    });

    const data = await res.json(project);
    console.log(data);
    console.log(project);
  };

and here is a small part of the form:

 <form onSubmit={handleSubmit}>
              <Box width="50%">
                <LocalizationProvider dateAdapter={AdapterDayjs}>
                  <DatePicker
                    id="project_start"
                    name="project_start"
                    value={project.project_start}
                    onChange={handleChange}
                    slotProps={{
                      textField: {
                        size: "small",
                        margin: "dense",
                      },
                    }}
                  />
                  <DatePicker
                    id="project_end"
                    name="project_end"
                    value={project.project_end}
                    onChange={handleChange}
                    renderValue={(selected) => {
                      return [{ selected }];
                    }}
                    slotProps={{
                      textField: { size: "small", margin: "dense" },
                    }}
                  />
                </LocalizationProvider>
                <TextField id="nombreP" margin="dense" size="small" />
                <FormControl size="small" sx={{ m: 1 }}>
                  <Select
                    id="encargadoP"
                    multiple
                    name="usersId"
                    value={project.usersId}
                    onChange={handleChange}
                    MenuProps={MenuProps}
                    renderValue={(selected) => (
                      <Box sx={{ display: "flex", flexWrap: "wrap", gap: 0.5 }}>
                        {selected.map((value) => (
                          <Chip key={value} label={value} />
                        ))}
                      </Box>
                    )}
                    sx={{ width: 205 }}
                  >
                    {data?.map(({ _id, name }) => (
                      <MenuItem key={_id} value={name}>
                        {name}
                      </MenuItem>
                    ))}
                  </Select>
                </FormControl>
              </Box>
            </Box>
            <Button
              type="submit"
              variant={"outlined"}
              size={"large"}
              sx={{
                width: 420,
                border: "1px solid white",
                m: "3rem 0 0 0",
                color: "white",
                borderRadius: "30px",
              }}
            >
              Agregar proyecto
            </Button>
          </Box>
        </form>

I am very grateful to everyone who can help me e.preventDefault is not a function (React) 不要翻译。

英文:

I am using React to make a form that will make a POST to MongoDB, what happens is that when I start trying to fill the form I get the following error:

e.preventDefault is not a function

TypeError: e.preventDefault is not a function
at handleChange

this is the code (in a summarized form), this is handleChange and handleSubmit:

 const handleChange = (e) =&gt; {
    e.preventDefault();
    setProject({ ...project, [e.target.name]: e.target.value });
  };

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

    const res = await fetch(&quot;http://localhost:5001/client/projects&quot;, {
      method: &quot;POST&quot;,
      body: JSON.stringify(project),
      headers: { &quot;Content-Type&quot;: &quot;application/json&quot; },
    });

    const data = await res.json(project);
    console.log(data);
    console.log(project);
  };

and here is a small part of the form:

 &lt;form onSubmit={handleSubmit}&gt;
              &lt;Box width=&quot;50%&quot;&gt;
                &lt;LocalizationProvider dateAdapter={AdapterDayjs}&gt;
                  &lt;DatePicker
                    id=&quot;project_start&quot;
                    name=&quot;project_start&quot;
                    value={project.project_start}
                    onChange={handleChange}
                    slotProps={{
                      textField: {
                        size: &quot;small&quot;,
                        margin: &quot;dense&quot;,
                      },
                    }}
                  /&gt;
                  &lt;DatePicker
                    id=&quot;project_end&quot;
                    name=&quot;project_end&quot;
                    value={project.project_end}
                    onChange={handleChange}
                    renderValue={(selected) =&gt; {
                      return [{ selected }];
                    }}
                    slotProps={{
                      textField: { size: &quot;small&quot;, margin: &quot;dense&quot; },
                    }}
                  /&gt;
                &lt;/LocalizationProvider&gt;
                &lt;TextField id=&quot;nombreP&quot; margin=&quot;dense&quot; size=&quot;small&quot; /&gt;
                &lt;FormControl size=&quot;small&quot; sx={{ m: 1 }}&gt;
                  &lt;Select
                    id=&quot;encargadoP&quot;
                    multiple
                    name=&quot;usersId&quot;
                    value={project.usersId}
                    onChange={handleChange}
                    MenuProps={MenuProps}
                    renderValue={(selected) =&gt; (
                      &lt;Box sx={{ display: &quot;flex&quot;, flexWrap: &quot;wrap&quot;, gap: 0.5 }}&gt;
                        {selected.map((value) =&gt; (
                          &lt;Chip key={value} label={value} /&gt;
                        ))}
                      &lt;/Box&gt;
                    )}
                    sx={{ width: 205 }}
                  &gt;
                    {data?.map(({ _id, name }) =&gt; (
                      &lt;MenuItem key={_id} value={name}&gt;
                        {name}
                      &lt;/MenuItem&gt;
                    ))}
                  &lt;/Select&gt;
                &lt;/FormControl&gt;
              &lt;/Box&gt;
            &lt;/Box&gt;
            &lt;Button
              type=&quot;submit&quot;
              variant={&quot;outlined&quot;}
              size={&quot;large&quot;}
              sx={{
                width: 420,
                border: &quot;1px solid white&quot;,
                m: &quot;3rem 0 0 0&quot;,
                color: &quot;white&quot;,
                borderRadius: &quot;30px&quot;,
              }}
            &gt;
              Agregar proyecto
            &lt;/Button&gt;
          &lt;/Box&gt;
        &lt;/form&gt;

I am very grateful to everyone who can help me e.preventDefault is not a function (React) 不要翻译。

答案1

得分: 0

The onChange 回调函数对于 MUI 的 DatePicker 使用新值作为第一个参数,而不是事件对象。

因此,你的代码应该更改为类似于以下方式:

const handleChange = (name, newVal) => {
    setProject({ ...project, [name]: newVal });
};
<DatePicker id="project_start" name="project_start" value={project.project_start}
      onChange={handleChange.bind(null, 'project_start')}/>
{ /* 其他的日期选择器也是如此 ... */ }
英文:

The onChange callback for MUI's DatePicker is called with the new value as the first argument, not the event object.

Thus your code should be changed to something like this:

const handleChange = (name, newVal) =&gt; {
    setProject({ ...project, [name]: newVal });
};
&lt;DatePicker id=&quot;project_start&quot; name=&quot;project_start&quot; value={project.project_start}
      onChange={handleChange.bind(null, &#39;project_start&#39;)}/&gt;
{ /* and so on for the other DatePickers ... */ }

huangapple
  • 本文由 发表于 2023年4月11日 03:52:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75980246.html
匿名

发表评论

匿名网友

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

确定