我有几个状态,我只想为所有状态编写 1 个处理程序。更改时,只有 1 个处理程序会触发并仅在状态发生更改时更改状态。
我有这样的基于类的组件的解决方案
handleChange (evt) {
this.setState({ [evt.target.name]: evt.target.value });
}
Run Code Online (Sandbox Code Playgroud)
但是当我使用基于函数的组件时,我不知道如何使用。
export default function DateBar(props)
{
let date = new Date();
let i = 1;
const days = Array.from({length: new Date(date.getYear(), date.getMonth() + 1, 0).getDate()}).map(() => <option key = {uuidv4()} value={i}>{i++}</option>);
const monthOpts = props.months.map(month => <option key = {uuidv4()} value={month} name = {month} id = {month}>{month}</option>);
const yearsOpts = props.years.map(year => <option key = {uuidv4()} value ={year} name = {year} id = {year}>{year}</option>);
// states …Run Code Online (Sandbox Code Playgroud)