gpt4 book ai didi

reactjs - 管理 React 中多个下拉菜单的状态(语义 UI)

转载 作者:行者123 更新时间:2023-12-02 03:51:59 25 4
gpt4 key购买 nike

我正在关注文档 here创建受控组件下拉菜单。

我如何修改我的handleChange函数以使其知道它正在从“选择成员”下拉列表或“选择日期”下拉列表中接收值,以便我可以相应地更新状态值(“成员”) ”和“小时)?

import React, { Component } from 'react'
import { Dropdown, Grid } from 'semantic-ui-react'

class DropdownExampleRemote extends Component {
componentWillMount() {
this.setState({
optionsMembers: [
{ key: 1, text: 'DAILY', value: 'DAILY' },
{ key: 2, text: 'MONTHLY', value: 'MONTHLY' },
{ key: 3, text: 'WEEKLY', value: 'WEEKLY' },
],
optionsDays: [
{ key: 1, text: 'SUNDAY', value: 'SUNDAY' },
{ key: 2, text: 'MONDAY', value: 'MONDAY' },
{ key: 3, text: 'TUESDAY', value: 'TUESDAY' },
],
value: '',
member: '',
hour: '',
})
}

handleChange = (e, { value }) => {
this.setState({ member: value })
}

render() {
const {optionsMembers, optionsDays, value } = this.state

return (
<Grid>
<Grid.Column width={6}>
<Dropdown
selection
options={optionsMembers}
value={value}
placeholder='Select Member'
onChange={this.handleChange}
/>
</Grid.Column>
<Grid.Column width={6}>
<Dropdown
selection
options={optionsDays}
value={value}
placeholder='Select Day'
onChange={this.handleChange}
/>
</Grid.Column>
<Grid.Column width={4}>
<div>{this.state.member}</div>
<div>{this.state.day}</div>
</Grid.Column>
</Grid>
)
}
}

export default DropdownExampleRemote

更新:解决方案已有效更新状态。仍然不确定为什么保管箱的选择不坚持并默认为占位符。

最佳答案

我建议将您想要更新的值的名称传递到句柄更改函数中,例如:

import React, { Component } from 'react'
import { Dropdown, Grid } from 'semantic-ui-react'

class DropdownExampleRemote extends Component {
componentWillMount() {
this.setState({
optionsMembers: [
{ key: 1, text: 'DAILY', value: 'DAILY' },
{ key: 2, text: 'MONTHLY', value: 'MONTHLY' },
{ key: 3, text: 'WEEKLY', value: 'WEEKLY' },
],
optionsDays: [
{ key: 1, text: 'SUNDAY', value: 'SUNDAY' },
{ key: 2, text: 'MONDAY', value: 'MONDAY' },
{ key: 3, text: 'TUESDAY', value: 'TUESDAY' },
],
value: '',
member: '',
day: '',
})
}

handleChange = (value, key) => {
this.setState({ [key]: value });
}

render() {
const {optionsMembers, optionsDays, value, member, day } = this.state

return (
<Grid>
<Grid.Column width={6}>
<Dropdown
selection
options={optionsMembers}
value={member}
placeholder='Select Member'
onChange={(e,{value})=>this.handleChange(value, 'member')}
/>
</Grid.Column>
<Grid.Column width={6}>
<Dropdown
selection
options={optionsDays}
value={day}
placeholder='Select Day'
onChange={(e,{value})=>this.handleChange(value, 'day')}
/>
</Grid.Column>
<Grid.Column width={4}>
<div>{member}</div>
<div>{day}</div>
</Grid.Column>
</Grid>
)
}
}

export default DropdownExampleRemote

关于reactjs - 管理 React 中多个下拉菜单的状态(语义 UI),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45155334/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com