如何传递key:value onChange from reactjs中的select-option?

xkftehaa  于 2023-01-08  发布在  React
关注(0)|答案(2)|浏览(104)

我遵循this tutorial并尝试对其进行一些修改。
我有这个代码addRequestForm.js

import { BiPlus } from 'react-icons/bi'
import { useMutation, useQueryClient } from "react-query"
import { addRequest, getRequests } from "../../lib/helper"
import Bug from "./bugRequest"
import Success from "./successRequest"

export default function AddRequestForm({ formData, setFormData }) {

    const queryClient = useQueryClient()
    const addMutation = useMutation(addRequest, {
        onSuccess: () => {
            queryClient.prefetchQuery('requests', getRequests)
        }
    })

    const handleSubmit = (e) => {
        e.preventDefault();
        if (Object.keys(formData).length == 0) return console.log("Don't have Form Data");
        let { sector, contact_name } = formData;

        const model = {
            sector, contact_name
        }

        addMutation.mutate(model)
    }

    if (addMutation.isLoading) return <div>Loading!</div>
    if (addMutation.isError) return <Bug message={addMutation.error.message}></Bug>
    if (addMutation.isSuccess) return <Success message={"Added Successfully"}></Success>

    return (
        <form className="grid lg:grid-cols-2 w-4/6 gap-4 md:px-4 md:mx-auto" onSubmit={handleSubmit}>
            
            <div className="input-type">
                <label htmlFor="sector" className="block text-sm font-medium text-gray-700">
                    Sector
                </label>
                <select
                    id="sector"
                    name="sector"
                    autoComplete="sector-name"
                    className="border w-full px-5 py-3 focus:outline-none rounded-md"
                    
                    onChange={(e) => setFormData({ [e.target.name]: e.target.value })}
               
                >
                    <option value="North">North</option>
                    <option value="South">South</option>
                    <option value="West">West</option>
                    <option value="East">East</option>
                    {/*  */}
                </select>
            </div>

            <div className="form-control input-type">
                <label className="input-group">
                    <span>Contact Name</span>
                    <input type="text" onChange={setFormData} name="contact_name" placeholder="Contact Name Here..“ className="w-full input input-bordered" />
                </label>
            </div>


            <button type="submit" className="flex justify-center text-md w-2/6 bg-green-500 text-white px-4 py-2 border rounded-md hover:bg-gray-50 hover:border-green-500 hover:text-green-500">
                Add <span className="px-1"><BiPlus size={24}></BiPlus></span>
            </button>

        </form >
    )

}

对于name=“contact_name”已经成功插入数据库(mongodb + mongoose),但是,我还是不明白,如何对select-option做同样的操作?
已经尝试过这些:

onChange={(e) => setFormData({ e.target.name: e.target.value })}
onChange={(e) => setFormData({ sector.name: e.target.value })}
onChange={(e) => setFormData({ [e.target.name][0]: e.target.value })}

出现语法错误:意外的标记,应为“,"。虽然我认为这是正确的值,但我正在控制这些(结果:“扇区:我的名字”)。

onChange={(e) => setFormData({ sector: e.target.value })}
onChange={(e) => setFormData({ [e.target.name]: e.target.value })}

没有插入数据库。
如何正确地编写它?我使用的是nextjs(我认为reactjs也是一样的,对吗?)+ @reduxjs/toolkit 1.9.1.
如果需要,这里是reducer.js:

import UpdateRequestForm from "./updateRequestForm";
import AddRequestForm from "./addRequestForm";
import { useSelector } from "react-redux";
import { useReducer } from "react";

const formReducer = (state, event) => {
    return {
        ...state,
        [event.target?.name]: event.target?.value
    }
}

export default function FormRequest() {

    const [formData, setFormData] = useReducer(formReducer, {})
    const formId = useSelector((state) => state.app.client.formId)

    return (
        <div className="container mx-auto py-5">
            {formId ? UpdateRequestForm({ formId, formData, setFormData }) : AddRequestForm({ formData, setFormData })}
        </div>
    )
}
7d7tgy0s

7d7tgy0s1#

试试这个

onChange = {(e) => { 
                          let obj = {} ; 
                          obj[e.target.name] = e.target.value;
                          setFormData(obj);
                
                       } 
                    }
wtzytmuj

wtzytmuj2#

我没有完全理解你的reducer用例,但是你可以用一个简单的handleChange函数来代替它,如下所示。顺便说一句,调用setFormData(//newData)将完全擦除其中的内容,所以我们首先扩展原始状态,并基于key更改值。
状态和句柄更改:

const [formData, setFormData] = React.useState({
    sector: "North",
    contact_name: ""
  });

const handleChange = ({ target }) => {
  // Deconstruct the target
  // Deconstruct the name and value from the target
  const { name, value } = target;
  // Set the state
  setFormData((previousState) => ({
    ...previousState,
    [name]: value,
  }));
};

用法:

<select
    placeholder="Select a sector"
    name="sector"
    value={formData.sector}
    onChange={handleChange}
  >
    <option value="North">North</option>
    <option value="South">South</option>
    <option value="West">West</option>
    <option value="East">East</option>
  </select>

  <input
    value={formData.contact_name}
    placeholder="Contact Name"
    name="contact_name"
    onChange={handleChange}
  />

代码和框:https://codesandbox.io/s/interesting-kilby-emcyl4?file=/src/App.js:461-615
还需要注意的是,你需要给input元素分配一个名称,在这个例子中,name被设置为sector,因为name值将被用来指示state对象中的哪个键必须被更新。
控制输入字段是一个很好的实践。因此,将输入值设置为您的状态将实现这一点。唯一要记住的是,您需要一个默认状态。如果您想查看一下,我已经将其全部添加到codesandbox中。

相关问题