我正在尝试在箭头功能组件中设置setState,但出现错误“未定义setState”。
我尝试使用setState({ selectedSlot })
和this.setState({ selectedSlot })
在handleChange中设置状态,但没有任何效果。
const AddAssetActivity = props => {
let { variations, slots, priceStructure } = props;
let state = {
selectedSlot: { "0": "00", "1": "11" },
cal: 1
};
let handleChange = (event, value) => {
let selectedSlot = state.selectedSlot;
selectedSlot[value.variation] = value.slot;
setState({ selectedSlot });
};
return (
<div>
</div>
);
};
最佳答案
要在功能组件内部实现状态逻辑,请使用hooks
:
const AddAssetActivity = props => {
let { variations, slots, priceStructure } = props;
const [state, setState] = React.useState({
selectedSlot: { "0": "00", "1": "11" },
cal: 1
})
let handleChange = (event, value) => {
const _state = {...state}
let selectedSlot = _state.selectedSlot;
selectedSlot[value.variation] = value.slot;
setState({ ..._state, selectedSlot });
};
return (
<div>
</div>
);
};