简体   繁体   English

为什么在 useState 挂钩中使用 const 时不显示错误

[英]Why it is not showing error, when using const in useState hook

This is my complete React js code.这是我完整的 React js 代码。 I am using const in "const [name, setName] =useState('Micheal');".我在“const [name, setName] =useState('Micheal');”中使用 const。 It is not showing error, when I am changing name using setName function.当我使用 setName function 更改名称时,它没有显示错误。 Since const in javascript means, it cannot be unchanged, why it is not showing any error由于 javascript 中的 const 表示,它不能改变,为什么它没有显示任何错误

import React,{useState} from "react"
import ReactDOM from 'react-dom'
function Emp(){
  const [name,setName]=useState("Micheal");
  const [id,setId]=useState(1024);
  const [age,setAge]=useState(23);
  const handleClick=()=>{
    setName("Jackson");
    setId(2021);
    setAge(29);
  }
  return (
    <>
    <h1>Emp Details</h1>
    <h1>your name: {name}</h1>
    <h1>your id: {id}</h1>
    <h1>your age: {age}</h1>
    <button type="button" onClick={handleClick}>Click Here</button>
    </>
  );
}
ReactDOM.render(<Emp/>,document.getElementById('root'));

const means that the identifier can't be reassigned. const表示不能重新分配标识符。 (Not to be confused with immutable values. Unlike true immutable datatypes such as those produced by Immutable.js and Mori, a const object can have properties mutated.) (不要与不可变值混淆。与真正的不可变数据类型(例如由 Immutable.js 和 Mori 生成的数据类型)不同,一个const object 可以具有改变的属性。)

From: https://medium.com/javascript-scene/javascript-es6-var-let-or-const-ba58b8dcde75来自: https://medium.com/javascript-scene/javascript-es6-var-let-or-const-ba58b8dcde75

For example:例如:

 const obj1 = {}; obj1.a = '12345' console.log(obj1)

Check out this example from the React docs: https://reactjs.org/docs/hooks-state.html#recap从 React 文档中查看这个示例: https://reactjs.org/docs/hooks-state.html#recap

Specifically, this line: Line 9: When the user clicks, we call setCount with a new value. React will then re-render the Example component, passing the new count value to it.具体来说,这一行: Line 9: When the user clicks, we call setCount with a new value. React will then re-render the Example component, passing the new count value to it. Line 9: When the user clicks, we call setCount with a new value. React will then re-render the Example component, passing the new count value to it.

Every time you call setId, for example, useState will return a new value for id, and re-render your component.例如,每次调用 setId 时,useState 都会为 id 返回一个新值,并重新渲染您的组件。 It is not simply assigning a variable - useState has side effects, and does more than just assign a value.它不仅仅是分配一个变量 - useState 有副作用,而且不仅仅是分配一个值。

This is not in violation of the use of const , because useState is technically returning a newly initialized value and re-rendering with that value.这并不违反const的使用,因为 useState 在技术上返回一个新初始化的值并使用该值重新渲染。 It is not double assigning, renaming or anything like that.它不是双重分配、重命名或类似的东西。

Lastly, const also prevents you from mistakenly re-assigning the useState variables within your component.最后, const还可以防止您错误地重新分配组件中的 useState 变量。

setState is API hook of React that we call it this way: const [name, setName] =useState('Micheal'); setState是 React 的 API 钩子,我们这样称呼它: const [name, setName] =useState('Micheal');

and this is destructuring assignment, name is a constant referencing to object behind state called Micheal by you and setName is a function to notice to React state that the Micheal will have change and the Micheal will have new value after that, this way the whole component will rerender again... and this is destructuring assignment, name is a constant referencing to object behind state called Micheal by you and setName is a function to notice to React state that the Micheal will have change and the Micheal will have new value after that, this way the whole component将再次渲染...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么 React Hook useState 使用 const 而不是 let - Why React Hook useState uses const and not let 为什么我收到“未捕获的错误:重新渲染次数过多”。 在尝试使用 useState 将一个从自定义挂钩中获得的常量分配给另一个常量时? - Why I am getting 'Uncaught Error: Too many re-renders.' while a try to assign a const that a got from custom hook into another const using useState? 为什么在 useState Varaible Declare 时不使用 const? 显示错误 - why const not use when useState Varaible Declare ?? Show Error 使用 useState、React 时 Hook 调用无效 - Invalid Hook call when using useState, React 为什么useState变量`const`在反应? - Why are useState variables `const` in react? 更新打字稿版本时反应 useState 钩子上的打字稿错误 - Typescript error on react useState hook when updating typescript version 使用 useState 钩子时反应组件渲染两次 - React component rendering twice when using useState hook 使用 React useState 钩子时无法获取更新的 state - Unable to get the updated state when using React useState hook 使用 useState Hook 时,如何使用之前的 state 重构 if else if? - How to refactor an if else if with previous state when using useState Hook? 使用 useState Hook 引用函数状态时关闭 - Closure when reference to function state using useState Hook
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM