使用新日期更新表单状态日期

发布于 2025-02-10 08:33:13 字数 1362 浏览 2 评论 0原文

我在形式组件中使用了两个不同的状态。一个用于捕获所有文本输入,另一个是使用材料UI的datePicker组件捕获日期。

我想在我的主要输入中添加日期状态,并根据日期状态进行更新。

这是我正在使用的代码:

const [date, setDate] = useState(null);
const [inputValues, setInputValues] = useState({
  title: "",
  category: "",
  description: "",
  city: "",
  venue: "",
  date: "",
});

const handleChange = (event) => {
  setInputValues({
    ...inputValues,
    [event.target.name]: event.target.value,
    date: date,
  });
};

<TextField
  variant="outlined"
  label="Event title"
  size="small"
  fullWidth
  name="title" // name references the 'input' element attribute
  value={inputValues.title} // value references the 'input' attribute 'value'
  onChange={handleChange} // onChange will pass the event by default
></TextField>
<DatePicker
  value={date} // DatePicker requires onChange and value
  onChange={(newDate) => setDate(newDate)} // Cannot be set at the TextField level
  renderInput={(params) => (
    <TextField
      {...params}
      size="small"
      fullWidth
      variant="outlined"
    ></TextField>
  )}
></DatePicker>

不确定我是否需要使用使用将两个状态链接在一起?任何指导都将不胜感激。

谢谢!

I'm using two different states in my Form component. One to capture all the text inputs and one to capture the Date using the DatePicker component from Material UI.

I want to add a date state in my main input and update it based on the date state.

Here's the code that I'm using:

const [date, setDate] = useState(null);
const [inputValues, setInputValues] = useState({
  title: "",
  category: "",
  description: "",
  city: "",
  venue: "",
  date: "",
});

const handleChange = (event) => {
  setInputValues({
    ...inputValues,
    [event.target.name]: event.target.value,
    date: date,
  });
};

<TextField
  variant="outlined"
  label="Event title"
  size="small"
  fullWidth
  name="title" // name references the 'input' element attribute
  value={inputValues.title} // value references the 'input' attribute 'value'
  onChange={handleChange} // onChange will pass the event by default
></TextField>
<DatePicker
  value={date} // DatePicker requires onChange and value
  onChange={(newDate) => setDate(newDate)} // Cannot be set at the TextField level
  renderInput={(params) => (
    <TextField
      {...params}
      size="small"
      fullWidth
      variant="outlined"
    ></TextField>
  )}
></DatePicker>

Not sure if I need to use useEffect to link the two states together? Any guidance would be greatly appreciated.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

玉环 2025-02-17 08:33:13

与其尝试更改其他输入的同时更新日期,不如会在每次通过

当将现有状态数据与新状态数据相结合时,您还应将参数的功能形式用于setStatesetInputValues) 't设置过时的数据。

例如

  • this:setState(state =&gt;({... state,somenewvalue}))
  • 而不是:setState({... state,somenewvalue})

Code in TypeScript Playground

import {useEffect, useState} from 'react';

function Component () {
  const [date, setDate] = useState(null);

  const [inputValues, setInputValues] = useState({
    title: "",
    category: "",
    description: "",
    city: "",
    venue: "",
    date: "",
  });

  useEffect(() => {
    if (typeof date !== 'string') return;
    setInputValues(state => ({...state, date}));
  }, [date]);

  const handleChange = (event) => {
    setInputValues(state => ({
      ...state,
      [event.target.name]: event.target.value,
    }));
  };

  // Rest of component...
}

Instead of trying to update the date at the same time the other inputs are changed, do that in an effect that will take care of it automatically every time the date is updated by including it in the dependency array of the effect.

You should also use the functional form of the argument to setState (setInputValues in your code) when combining existing state data with new state data, to guarantee that you aren't setting stale data.

e.g.

  • This: setState(state => ({...state, someNewValue}))
  • Instead of: setState({...state, someNewValue})

Code in TypeScript Playground

import {useEffect, useState} from 'react';

function Component () {
  const [date, setDate] = useState(null);

  const [inputValues, setInputValues] = useState({
    title: "",
    category: "",
    description: "",
    city: "",
    venue: "",
    date: "",
  });

  useEffect(() => {
    if (typeof date !== 'string') return;
    setInputValues(state => ({...state, date}));
  }, [date]);

  const handleChange = (event) => {
    setInputValues(state => ({
      ...state,
      [event.target.name]: event.target.value,
    }));
  };

  // Rest of component...
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文