简体   繁体   English

React Select 在 onChange 的参数中传递多个选项

[英]React Select pass multiple options in parameter on onChange

I am using React Select in my project and i have come across a situation where i need to loop through an array and show Select option in each loop.我在我的项目中使用 React Select,我遇到了一种情况,我需要遍历一个数组并在每个循环中显示 Select 选项。

          {Array.apply(null, { length: 3 }).map((item, index) => (
             
              <div className="wrapper-person-2">
                <p className="tab-text">Profissão</p>
                <Select
                  value={calcFormikValuesSelect()}
                  name={`relation[${index}]`}
                  onChange={handleChange}
                  options={options}
                  className="select-smoker"
                />
    
              </div>

          ))}

now for onChange i was using function which is mentioned in the docs现在对于 onChange 我正在使用文档中提到的函数

 const handleChange = selectedOption => {
    this.setState({ selectedOption });
    console.log(`Option selected:`, selectedOption);
  };

but the issue is i need to pass my "index" value to onChange function too for my work, so i looked onto passing multiple parameters on onChange but sadly found nothing useful :(但问题是我的工作也需要将我的“索引”值传递给 onChange 函数,所以我查看了在 onChange 上传递多个参数,但遗憾的是没有发现任何有用的 :(

Hope someone helps me out, thanks !希望有人帮帮我,谢谢!

To pass multiple parameters to handleChange just call it in this way:要将多个参数传递给handleChange只需以这种方式调用它:

      {Array.apply(null, { length: 3 }).map((item, index) => (
         
          <div className="wrapper-person-2">
            <p className="tab-text">Profissão</p>
            <Select
              value={calcFormikValuesSelect()}
              name={`relation[${index}]`}
              onChange={(selectedOption) => handleChange(selectedOption, index)}
              options={options}
              className="select-smoker"
            />

          </div>

      ))}

Then:然后:

const handleChange = (selectedOption, index) => {
    this.setState({ selectedOption });
    console.log(`Option selected:`, selectedOption);
    console.log(`Index selected:`, index);
  };

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

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