替换一个对象内的钩子 useState

独自的
  const [country, setCountry] = useState("");
  const [city, setCity] = useState("");
  const [population, setPopulation] = useState("");
  const [location, setLocation] = useState("");
  const [temp_min, setTmep_min] = useState("");

嘿,任何人都知道如何以有效的方式替换这些钩子 useState 并清理它的代码,例如将它们全部放入一个对象中,而不是使用新的 useState 对其进行初始化。

气体

您可以创建一个对象:

const [data, setData] = useState({
  country: '',
  city: '', 
  population: 0, // some number here
  location: '', 
  temp_min: 0, // some number here
});

然后你可以像这样获得每个值:

console.log(data.country)
console.log(data.city)
// and so on..

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章