简体   繁体   English

使用带有多个输入的 react-select

[英]Using react-select with multiple inputs

I have multiple react-select inputs, each have their own separate options array.我有多个反应选择输入,每个输入都有自己单独的选项数组。 The issue i'm having is I'm now sure how to properly store the output of each select input.我遇到的问题是我现在确定如何正确存储每个 select 输入的 output。

I want to use this select output to POST to my backend but i'm unsure how to do it all with a single handler function.我想使用这个 select output 来 POST 到我的后端,但我不确定如何使用单个处理程序 function 完成所有操作。

This is what I have so far, i have 2 paragraphs to just output the appropriate result from the select fields but i can't seem to get it working.这就是我到目前为止所拥有的,我有 2 段只是 output 来自 select 字段的适当结果,但我似乎无法让它工作。

This is the codesandbox i have:这是我拥有的代码框:

https://codesandbox.io/s/busy-yonath-rlj9e?file=/src/App.js https://codesandbox.io/s/busy-yonath-rlj9e?file=/src/App.js

In your code you are using the useState() hook and everytime a user selects an option, you are setting your state variable to the selected value.在您的代码中,您正在使用 useState() 挂钩,并且每次用户选择一个选项时,您都将 state 变量设置为所选值。 The problem is that everytime you run the setSelectOptions({ e }) function, you are overwriting the existing data with the value of 'e'.问题是每次运行setSelectOptions({ e }) function 时,都会用“e”的值覆盖现有数据。

What you can do is: Create a selectHandler function that takes in 2 arguments (the new value and the value it corresponds to in the state variable)您可以做的是:创建一个 selectHandler function 接受 2 个 arguments (新值及其在 state 变量中对应的值)

The code will look something like this:代码将如下所示:

 import "./styles.css"; import Select from "react-select"; import { useEffect, useState } from "react"; export default function App() { const [selectOptions, setSelectOptions] = useState({ brand: "", model: "", color: "", bodyType: "", fuelType: "", transmission: "", location: "" }); const brandOptions = [ { value: "bmw", label: "Bmw" }, { value: "audi", label: "Audi" }, { value: "toyota", label: "Toyota" }, { value: "nissan", label: "Nissan" } ]; const transmissionOptions = [ { value: "automatic", label: "Automatic" }, { value: "manual", label: "Manual" } ]; useEffect(() => console.log(selectOptions), [selectOptions]) const handleChange = (e, type) => { setSelectOptions((previousState) => ({...previousState, [type]: e.value, })); }; return ( <div className="App"> selected brand: {selectOptions.brand} <br /> selected transmission:{selectOptions.transmission} <div className="mb-2" style={{ width: "40%", margin: "0 auto" }}> <Select value={selectOptions.brand} onChange={(e) => handleChange(e, "brand")} placeholder="Select brand" options={brandOptions} /> <Select value={selectOptions.transmission} onChange={(e) => handleChange(e, "transmission")} placeholder="Select transmission" options={transmissionOptions} /> </div> </div> ); }

Just as an explanation, all I am doing in the setSelectOptions() function is passing in the previous values of the state variable and updating the value coinciding to the select field.作为解释,我在 setSelectOptions() function 中所做的只是传递 state 变量的先前值并更新与 Z99938282F04071854241E18ZF16EF 字段一致的值。

Note: Insert this code into your project, I ran it and it worked so let me know if it did help!注意:将此代码插入您的项目中,我运行它并且它有效,所以让我知道它是否有帮助!

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

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