【发布时间】:2020-08-03 03:47:27
【问题描述】:
我最近开始大量使用 react hooks。
使用“React.useState”时,状态管理对我来说似乎更直观。
无论如何,虽然它工作正常,但我知道我开始保存到 state 的值越多,它就会开始变得混乱。
例如,随着我的汽车零件应用程序的发展,它现在看起来像这样:
const [isShown, setIsShown] = React.useState(false);
const [idVal, setIdValue] = React.useState(false);
const [partNameVal, setPartNameValue] = React.useState(false);
const [engineEngineEngineTypeVal, setEngineEngineTypeValue] = React.useState(false);
const [displacementVal, setDisplacementValue] = React.useState(false);
const [makeVal, setMakeValue] = React.useState(false);
const [countryVal, setCountryValue] = React.useState(false);
const hide = () => setIsShown(false);
const show = (id, partName, engineEngineType, displacement, department, country) => {
setIsShown(true);
setIdValue(id);
setPartNameValue(partName);
setEngineTypeValue(engineEngineType);
setDisplacementValue(displacement);
setMakeValue(department);
setCountryValue(country);
}
<p>ID: {idVal}</p>
<p>PartName: {partNameVal}</p>
<p>EngineType: {engineEngineTypeVal}</p>
<p>Displacement: {displacementVal}</p>
<p>Make: {makeVal}</p>
<p>Country: {countryVal}</p>
我想知道是否有办法让它更易读,但仍然非常直观。
谢谢!
【问题讨论】:
标签: reactjs react-hooks react-state-management