React-select 选项没有得到 selected

React-select option not getting selected

我正在使用 react-select 包。当我 select 一个下拉选项时,我的 onChange 操作正常工作并更新状态。但是选项没有得到 selected。意思是,它不显示映射到状态的值,而只显示默认占位符。

我想我遗漏了一些小东西,但我对 React 和 redux 有一些新的了解,但我不确定这可能是什么。提前感谢您的帮助:)

Body.js

<Select id="input-field" value={this.props.create.selectedOption} onChange={this.props.addOption}
   options={[
      { value: 'navigate', label: 'Navigate to <URL>' },
      { value: 'enter', label: 'I enter <InputValue> to the <ElementKey>' },
      { value: 'click', label: 'Click on <ElementKey>' },
      ]}
/>


const mapStateToProps = (state, ownProps) => {
    return {
        create: state.reducerCreate
    }
}

Reducer.js

const initialState = {
    feature: '',
    scenarios: [],
    activeIndex: '',
    selectedOption: ''
}

回答我自己的问题时,我发现仅传递 selected 选项 label/value 是不够的,我们需要传递整个对象;

{ value: 'navigate', label: 'Navigate to <URL>' }

然后我们将 select 标签的值属性映射到州的值。

已更新Body.js

<Select id="input-field" componentClass="select" value={this.props.create.selectedOption.value} onChange={this.props.addOption}


const mapDispatchToProps = (dispatch, ownProps) => {
    return {
        addOption: (option) => {
            console.log(option)
            dispatch(addOption(option))
        }
    };
};