开发者问题收集

如何在 React 中获取键值对的值?

2020-02-13
99

我有以下场景: 表单中其中一个输入字段的值取决于下拉列表选择。例如,如果我选择“衬衫”,我想用衬衫图片的 URL 填充输入字段。我有以下代码。我需要帮助选择与用户选择相对应的正确值:

class ProductAdd extends React.Component{

constructor(){
    super();
    this.state = { defaultPrice: '$',
                    categoryValue: '',
                    URL: [
                        {
                            shirts: "https://www.istockphoto.com/photo/formal-shirt-with-button-down-collar-isolated-on-white-gm856917576-141225609",
                            jeans: "https://www.istockphoto.com/photo/blue-jeans-isolated-with-clipping-path-gm600373506-103229995",
                            jackets: "https://www.istockphoto.com/photo/black-hoodie-mock-up-gm695933044-128721993",
                            sweaters: "https://www.istockphoto.com/photo/formal-shirt-with-button-down-collar-isolated-on-white-gm856917576-141225609",
                            accessories: "https://www.shutterstock.com/image-vector/hair-accessories-woman-items-stylist-salon-1451306021"
                        }
                    ],
                };
    this.handleSubmit = this.handleSubmit.bind(this);
    this.handleChange = this.handleChange.bind(this);     
}

handleSubmit(e){
    e.preventDefault();
    const form = document.forms.productAdd;
    const product = {
        productName: form.productName.value, 
        category: form.category.value,
        price: form.price.value,
        image: form.image.value
        }
        this.props.createProduct(product);
        form.productName.value="";
        form.category.value="";
        form.price.value="";
        form.image.value="";
    }

handleChange(e){
    this.setState( { defaultPrice: e.target.value });
}



render(){
    let btnClass = [
        'btn',
        'clearfix'
      ]
      btnClass = btnClass.join(' ');

      Object.keys(this.state.URL).map(i => alert(this.state.URL['shirts']));

      //console.log(this.state.URL.shirts);

    return(
        <div>
            <form name="productAdd" onSubmit={this.handleSubmit} className="form">
                <div className="div1">
                    Category <br/>
                    <select name="category" className="selectBox"  onChange={ (e) => this.setState( { categoryValue: e.target.value }) }>
                        <option value="shirts">Shirts</option>
                        <option value="jeans">Jeans</option>
                        <option value="jackets">Jackets</option>
                        <option value="sweaters">Sweaters</option>
                        <option value="accessories">Accessories</option>
                    </select><br/><br/>
                    Product Name <br/>
                    <input type="text" name="productName" /><br/><br/>
                </div>
                <div className="div2">
                    Price Per Unit <br/>
                    <input ref="price" type="text" name="price" onChange={ this.handleChange } value={this.state.defaultPrice} /><br/><br/>                    
                    Image URL<br/>
                    <input type="text" name="image" value={this.state.URL[this.state.categoryValue]} /><br/><br/>
                </div>

                <button className={btnClass}>Add Product</button>
            </form>
    </div>
    );
}

>

1个回答

您的 URL 属性是一个数组:

URL: [
  {
    shirts: "https://www.istockphoto.com/photo/formal-shirt-with-button-down-collar-isolated-on-white-gm856917576-141225609",
    jeans: "https://www.istockphoto.com/photo/blue-jeans-isolated-with-clipping-path-gm600373506-103229995",
    jackets: "https://www.istockphoto.com/photo/black-hoodie-mock-up-gm695933044-128721993",
    sweaters: "https://www.istockphoto.com/photo/formal-shirt-with-button-down-collar-isolated-on-white-gm856917576-141225609",
    accessories: "https://www.shutterstock.com/image-vector/hair-accessories-woman-items-stylist-salon-1451306021"
  }
]

因此,您要么需要将其转换为对象(可以将其用作映射),要么需要获取数组中的第一个项,然后选择正确的条目。

选项 1

URL: {
  shirts: "https://www.istockphoto.com/photo/formal-shirt-with-button-down-collar-isolated-on-white-gm856917576-141225609",
  jeans: "https://www.istockphoto.com/photo/blue-jeans-isolated-with-clipping-path-gm600373506-103229995",
  jackets: "https://www.istockphoto.com/photo/black-hoodie-mock-up-gm695933044-128721993",
  sweaters: "https://www.istockphoto.com/photo/formal-shirt-with-button-down-collar-isolated-on-white-gm856917576-141225609",
  accessories: "https://www.shutterstock.com/image-vector/hair-accessories-woman-items-stylist-salon-1451306021"
}

选项 2

this.state.URL[0][this.state.categoryValue]

如果您是 Javascript 新手,那么您可能会对方括号感到困惑。Javascript 相当不寻常,因为您可以 使用方括号访问对象属性 包含变量。

JDB
2020-02-13