简体   繁体   English

反应选择选项未被选中

[英]React-select option not getting selected

I am using the react-select package.我正在使用react-select包。 When i select a drop-down option, my onChange action works correctly and updates the state.当我选择一个下拉选项时,我的 onChange 操作可以正常工作并更新状态。 But the option is not getting selected.但是该选项没有被选中。 Meaning, it doesn't show the value which is mapped to the state, instead just the default placeholder.意思是,它不显示映射到状态的值,而只显示默认占位符。

I think i'm missing something small, but i'm some what new to react and redux and i'm not sure what this could be.我想我错过了一些小东西,但我对 react 和 redux 有一些新的认识,我不确定这可能是什么。 Thanks for the help in advance :)提前感谢您的帮助:)

Body.js正文.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减速器.js

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

Answering my own question, I found out that just passing the selected options label/value is not enough we need pass the entire object;在回答我自己的问题时,我发现仅传递选定的选项标签/值是不够的,我们需要传递整个对象;

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

Then we map our select tag's value attribute with the state's value.然后我们将我们的 select 标签的 value 属性映射到 state 的 value。

Updated Body.js ;更新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))
        }
    };
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM