【发布时间】:2018-10-08 17:43:29
【问题描述】:
我正在使用 react-select 在我的 UI 上启用多项选择。但是,我需要从这个 react-select 中获取道具,因为我会将选择值发送到后端。我想知道。如何将状态值保存在数组中。我做了一个console.log,我得到的值是
0: {value: "vanilla", label: "Vanilla"}
1: {value: "strawberry", label: "Strawberry"}
2: {value: "chocolate", label: "Chocolate"}.
但是,我只想要值标签,因为我会将这个确切的值(例如 vanilla)发送到后端。有什么建议么。非常感谢。 react-select 的 github link 是:
import React, { Component } from "react";
import { connect } from "react-redux";
import PropTypes from "prop-types";
import checkboxes from "./checkboxes";
import Checkbox from "./Checkbox";
import Select from "react-select";
const options = [
{ value: "chocolate", label: "Chocolate" },
{ value: "strawberry", label: "Strawberry" },
{ value: "vanilla", label: "Vanilla" }
];
class CreatePreferences extends Component {
state = {
selectedOption: null
};
handleChange = selectedOption => {
this.setState({ selectedOption });
console.log(`Option selected:`, selectedOption);
};
render() {
const { selectedOption } = this.state;
return (
<Select
value={selectedOption}
isMulti
onChange={this.handleChange}
options={options}
/>
);
}
}
CreatePreferences.propTypes = {
profile: PropTypes.object.isRequired
};
const mapStateToProps = state => ({
profile: state.profile
});
export default connect(mapStateToProps)(CreatePreferences);
【问题讨论】:
标签: javascript reactjs forms