【问题标题】:Set name in state along with its index in an iteration在状态中设置名称及其在迭代中的索引
【发布时间】:2022-09-29 21:05:32
【问题描述】:

我有一个迭代代码。我正在使用 react 和 antd select。

      {this.state.count.map((d, i) => (
          <Select
            placeholder={`Question #${i + 1}`}
            style={{ width: \'100%\' }}
             onChange={(e) => this.setState({securityQuestions{i}:e})} // here I have issue.. need to set name with index
             showSearch
             optionFilterProp=\"children\"
             filterOption={(input, option) =>                                                    
    option.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}
         filterSort={(optionA, optionB) =>              
    optionA.children.toLowerCase().localeCompare(optionB.children.toLowerCase())
                                                                    }>  
{this.state.securityQuestions && this.state.securityQuestions.map((d) => <Select.Option value={d.id}>{d.securityQuestion}</Select.Option>)}
                 </Select>
         ))}

我的代码是这样的。正如我在代码中提到的,我在“onchange”中有问题。我想设置名称及其索引。谁能帮我?

    标签: reactjs


    【解决方案1】:

    如果我理解正确,您的目标是最终得到这样的结果?:

    {securityQuestions1:e}
    

    在这种情况下,您可以使用括号表示法来引用具有值的属性,然后您的值只是一个插值字符串(模板文字):

    {[`securityQuestions${i}`]:e}
    

    或者,如果您更喜欢串联的字符串:

    {['securityQuestions' + i]:e}
    

    【讨论】:

      猜你喜欢
      • 2021-11-27
      • 2020-06-05
      • 1970-01-01
      • 2021-12-28
      • 1970-01-01
      • 2012-11-20
      • 2016-12-01
      • 2016-10-24
      • 1970-01-01
      相关资源
      最近更新 更多