在 reactjs 中填充选择选项 [英] populate select option in reactjs

查看:24
本文介绍了在 reactjs 中填充选择选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从 API 中填充 ReactJS 中的下拉选择选项,但经过多次尝试而不是获取我想要的值后,我只得到了一个字符串,其中所有值都连接在一起.我尝试以不同的方式格式化 JSON 文件,但没有任何结果.

https://codesandbox.io/s/populate-select-option-fu5gd?fontsize=14

解决方案

我已经为此更改了您的迭代器:

 <选择>{this.state.colours[1]?this.state.colours[1].color.map(item => {return ;}):<option>{''}</option>}</选择>

这是完整的 codesandbox 代码.>

I am trying to populate a dropdown select option in ReactJS from an API but after several attempts instead of getting the values that I want, I got only a single string with all the values concatenated. I tried to format the JSON file in different ways but without any results.

https://codesandbox.io/s/populate-select-option-fu5gd?fontsize=14

解决方案

I have changed your iterator for this:

    <select>
      {this.state.colours[1]?
        this.state.colours[1].color.map(item => {
        return <option>{item}</option>;
      })
    : <option>{''}</option>
    }
    </select>

Here is the complete codesandbox code.

这篇关于在 reactjs 中填充选择选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆