简体   繁体   English

为什么我不能在 React 中更改我的输入值?

[英]Why can't I change my input value in React?

I'm doing a recipe project and I want the user to add a recipe.我正在做一个食谱项目,我希望用户添加一个食谱。

I'll add data via input but I cannot make any changes on "video URL" and "ingCount" inputs.我将通过输入添加数据,但我无法对“视频 URL”和“ingCount”输入进行任何更改。 I do not understand why this is so.我不明白为什么会这样。 Other inputs work fine and they work smoothly.其他输入工作正常,并且工作顺利。 . .

I just can't enter values into ingCount and videoLink.我只是无法在ingCount 和 videoLink 中输入值。

*** Their names on the API are also correct. *** 他们在 API 上的名字也是正确的。

例子

const [addRecipe, setAddRecipe] = useState({
    id: "",
    title: "",
    description: "",
    image: "",
    category: "",
    time: 0,
    ingCount: 0,
    servings: 0,
    videoLink: "",
    ingredients: "",
    makeDetails: ""
  })

  const handleChange = (e) => {
    setAddRecipe({
      id: Math.random() * 100,
      ...addRecipe,
      [e.target.name]: e.target.value
    })
  }

  const handleSubmit = (e) => {
    e.preventDefault()
    setRecipes([...recipes, addRecipe])
    axios
      .post(
        "https://5fccb170603c0c0016487102.mockapi.io/api/recipes",
        addRecipe
      )
      .then((res) => {
        console.log(res)
      })
      .catch((err) => {
        console.log(err)
      })
    setAddRecipe(addRecipe)
    console.log(handleSubmit)
  }
<form onSubmit={handleSubmit}>
          <div>
            <input
              type="text"
              name="title"
              value={addRecipe.title}
              onChange={handleChange}
              placeholder="baslik giriniz"
            />
          </div>
          <div>
            <input
              type="text"
              name="description"
              value={addRecipe.description}
              onChange={handleChange}
              placeholder="tarif aciklamasi"
            />
          </div>
          <div>
            <input
              type="text"
              name="category"
              value={addRecipe.category}
              onChange={handleChange}
              placeholder="kategori"
            />
          </div>
          <div>
            <input
              type="number"
              name="time"
              value={addRecipe.time}
              onChange={handleChange}
              placeholder="süre"
            />
          </div>
          <div>
            <input
              type="number"
              name="malzeme"
              value={addRecipe.ingCount}
              onChange={handleChange}
              placeholder="malzeme sayısı"
            />
          </div>
          <div>
            <input
              type="number"
              name="servings"
              value={addRecipe.servings}
              onChange={handleChange}
              placeholder="kişilik"
            />
          </div>

          <div>
            <input
              type="text"
              name="ingredients"
              value={addRecipe.ingredients}
              onChange={handleChange}
              placeholder="malzemeler"
            />
          </div>
          <div>
            <input
              type="text"
              name="makeDetails"
              value={addRecipe.makeDetails}
              onChange={handleChange}
              placeholder="yapilisi"
            />
          </div>
          <div>
            <input
              type="text"
              name="image"
              value={addRecipe.image}
              onChange={handleChange}
              placeholder="resim"
            />
          </div>
          <div>
            <input
              type="text"
              name="video"
              value={addRecipe.videoLink}
              onChange={handleChange}
              placeholder="video"
            />
          </div>
          <button className="todoform__btn" type="submit">
            Tarifi ekle!
          </button>
        </form>

Those are the only two fields where the name doesn't match with the value property.这些是namevalue属性不匹配的仅有的两个字段。 For the ingCount you have malzeme as the name.对于ingCount ,您将malzeme作为名称。 And for videoLink you have video as the name.对于videoLink ,您以video作为名称。

So for those, when所以对于那些,当

    setAddRecipe({
      id: Math.random() * 100,
      ...addRecipe,
      [e.target.name]: e.target.value
    })
    

is called, you are setting a property that will never be used.被调用时,您正在设置一个永远不会使用的属性。

The name has to be same in input field and object name.输入字段中的名称必须与 object 名称相同。 Currently for these its different ->目前对于这些不同->

       <input
          type="text"
          name="video". //should be video Link
          value={addRecipe.videoLink}
          onChange={handleChange}
          placeholder="video"
        />

Update this it will resolve your issue.更新这个它将解决您的问题。 Also I will suggest you to use callback inside setState.另外我会建议你在 setState 中使用回调。 Otherwise all the fields might not get saved in the state.否则所有字段可能不会保存在 state 中。

setState (prev => ({...prev, key: value}) setState (prev => ({...prev, key: value})

You are setting state values using "name" attribute, for ingCount and video, "name" attributes in input fields are different than state hence its not getting updated.您正在使用“名称”属性设置 state 值,对于 ingCount 和视频,输入字段中的“名称”属性与 state 不同,因此它没有得到更新。

 <input type="number" name="ingCount" value={addRecipe.ingCount} onChange={handleChange} placeholder="malzeme sayısı" /> <input type="text" name="videoLink" value={addRecipe.videoLink} onChange={handleChange} placeholder="video" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM