将标签添加到react-select

时间:2019-01-16 09:37:27

标签: javascript reactjs redux bootstrap-4 react-redux

enter image description here我是react-js的新手。这是我要尝试的,

enter image description here

现在我尝试的是

const options = [
    { value: 'chocolate', label: 'Chocolate' },
    { value: 'strawberry', label: 'Strawberry' },
    { value: 'vanilla', label: 'Vanilla' }
];
handleChange = (selectedOption) => {
    this.setState({ selectedOption });
    console.log(`Option selected:`, selectedOption);
}
const { selectedOption } = this.state;

<div className="row" style={styles}>
    <div className="col-12 d-flex">
        <div className="col-md-4">
            <Select
            value={selectedOption}
            onChange={this.handleChange}
            options={options}
            />
        </div>
        <div className="col-md-4">
            <Select
            value={selectedOption}
            onChange={this.handleChange}
            options={options}
            />
        </div>
        <div className="col-md-4">
            <div className="add">
                <button type="button" class="btn btn-primary">Primary</button>
            </div>
            <div className="remove">
                <button type="button" class="btn btn-success">Success</button>
            </div>
        </div>
    </div>
</div>

所以,在这里,当我尝试添加标签时,它就不在一行中了,

有人可以帮我吗?

提前谢谢!

1 个答案:

答案 0 :(得分:1)

如果您的“值”属性有效,则“标签”将显示在第一行-确保您的selectedOption状态具有一个存在于您的选项值中的有效值-全部根据docs

您的代码应如下所示:

import React from 'react';
import Select from 'react-select';

const options = [
  { value: 'chocolate', label: 'Chocolate' },
  { value: 'strawberry', label: 'Strawberry' },
  { value: 'vanilla', label: 'Vanilla' }
];

class App extends React.Component {
  state = {
    selectedOption: null,
  }
  handleChange = (selectedOption) => {
    this.setState({ selectedOption });
    console.log(`Option selected:`, selectedOption);
  }
  render() {
    const { selectedOption } = this.state;

    return (
 <div className="row" style={styles}>
        <div className="col-12 d-flex">
            <div className="col-md-4">
                <Select
                value={selectedOption}
                onChange={this.handleChange}
                options={options}
                />
            </div>
            <div className="col-md-4">
                <Select
                value={selectedOption}
                onChange={this.handleChange}
                options={options}
                />
            </div>
            <div className="col-md-4">
                <div className="add">
                    <button type="button" class="btn btn-primary">Primary</button>
                </div>
                <div className="remove">
                    <button type="button" class="btn btn-success">Success</button>
                </div>
            </div>
        </div>
    </div>
      <Select
        value={selectedOption}
        onChange={this.handleChange}
        options={options}
      />
    );
  }
}