获取州的当前值下拉列表

问题描述 投票:0回答:2

我需要从状态到函数获取所选的当前值,但它对我来说并不适用于我做错的事情

<Dropdown
  placeholder="Select Friend"
  fluid
  selection
  options={accountsList}
  value={accountsList.value}
  onChange={this.handleChange}
/>

// onChange function 
onChange = (e, { value }) => this.setState({ value })

// function where i need to get value
const acc = this.state.accountsList.value
const year = this.state.years.value
const months = this.state.months.value
axios
  .get(`http://******/cabinet/invoice/${acc}/${year}/${months}`,
reactjs semantic-ui
2个回答
2
投票

没有完整和简约的代码,我最好的猜测是你在onChange组件中调用了错误的函数Dropdown。您还需要通过event.target.value检索值传递给更改。

由于您更新了嵌套对象的状态,因此valueaccountsList的子项,您必须深度克隆它。

像这样:

this.setState({ accountsList: { ...this.state.accountsList, value: newValue} });

在你的例子中:

<Dropdown
      placeholder="Select Friend"
      fluid
      selection
      options={accountsList}
      value={accountsList.value}
      onChange={this.handleChange}
/>

handleChange = (event) => {
     this.setState({ accountsList: { ...this.state.accountsList, value: event.target.value} });
};

0
投票

我敢说选择的当前值更可能存在于onChange = ({value}) => this.setState({value})中,即:它;它将位于回调的第一个参数中,而不是第二个参数。

什么是下拉列表库?

© www.soinside.com 2019 - 2024. All rights reserved.