简体   繁体   English

React.js state object 与 useState 挂钩?

[英]React.js state object vs useState hook?

I started React by learning about the useState hook to manage component states.我通过学习useState钩子来管理组件状态来开始 React。 However, I have recently discovered that it is possible to use a state object instead.但是,我最近发现可以改用 state object。 I'm confused, and can't seem to find many references on the internet to help me understand the difference.我很困惑,似乎无法在互联网上找到很多参考资料来帮助我理解差异。

What are the differences, if any between the following... And which one is preferred, if at all?以下之间有什么区别(如果有的话)......如果有的话,哪一个是首选?

Option 1 : State object:选项 1State object:

class User extends React.component {

  state = {
    username: 'Tyler'
  }

Option 2 : useState hook:选项 2useState挂钩:

class User extends React.component {

  const [state, setState] = useState({
        username: 'Tyler'
  })

So, there is a bit of misunderstanding here.所以,这里有点误会。

State Object, or this.state was used when React used classes. State Object 或 this.state 在 React 使用类时使用。

This approach had some downsides.这种方法有一些缺点。 React needed to add a lot stuff to the object(when class is initialized) to make it compatible to react. React 需要向对象添加很多东西(当 class 初始化时)以使其兼容反应。

Hence since React 16 or so, it added the ability to using functions as building blocks for the application.因此,从 React 16 左右开始,它增加了使用函数作为应用程序构建块的能力。 The older approach(using classes) stil works, but not encouraged.较旧的方法(使用类)仍然有效,但不鼓励。 Classes does have some additional functionality as of this moment ( https://reactjs.org/docs/hooks-faq.html#do-hooks-cover-all-use-cases-for-classes ).到目前为止,类确实具有一些附加功能( https://reactjs.org/docs/hooks-faq.html#do-hooks-cover-all-use-cases-for-classes )。 Error Boundary ( https://reactjs.org/docs/error-boundaries.html ) is one such thing.错误边界( https://reactjs.org/docs/error-boundaries.html )就是这样一种情况。

Before 16, functions could be used as components, but didn't have state.在 16 之前,函数可以用作组件,但没有 state。 With Hooks, the functions could have state as well.使用 Hooks,函数也可以有 state。

This does come with some restrictions and how-tos in regards to usage( https://reactjs.org/docs/hooks-rules.html )这确实有一些关于使用的限制和操作方法( https://reactjs.org/docs/hooks-rules.html

The best resource on hooks is the docs from react on the subject. hooks 上最好的资源是 react on the subject 的文档。

The last, probably most important pro is that the code is cleaner.最后,可能是最重要的优点是代码更干净。

PS the second example won't work, useState has to be within a function defn. PS 第二个例子不起作用,useState 必须在 function defn 内。

// Option 2: useState 
function User() {
  const [state, setState] = useState({
    username: 'Tyler'
  })
}

useState is part of a whole new concept in React (16.8+), that's called hooks. useState是 React (16.8+) 中一个全新概念的一部分,称为钩子。 Hooks are a more efficient and simple way to write in React. Hooks 是一种更高效、更简单的 React 编写方式。

Hooks solve pain points: Hooks 解决痛点:

  1. Managing State: Reusing logic between multiple components can lead to wrapper hell or deeply nested components.管理 State:在多个组件之间重用逻辑可能导致包装地狱或深度嵌套的组件。

  2. Side Effects: Unrelated mixed in logic in lifecycle methods can get repetitive, and cause unnecessary side effects.副作用:生命周期方法中不相关的逻辑混合可能会重复,并导致不必要的副作用。

  3. Optimization: Hooks might reduce your bundle size.优化:钩子可能会减少你的包大小。

Please read all in here: https://medium.com/better-programming/react-hooks-vs-classes-add2676a32f2请在此处阅读所有内容: https://medium.com/better-programming/react-hooks-vs-classes-add2676a32f2

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

相关问题 React.js:useState Hook Setter 未更新 state - React.js: useState Hook Setter is not updating the state React.js useState Hook 需要一段时间才能更新 state - React.js useState Hook takes a while to update the state 如何在 react.js 的回调中调用 UseState 钩子的 state 更新程序 function - how to call UseState hook's state updater function inside a callback in react.js 当 useState 钩子改变状态时 React.js 组件不会重新渲染子组件 - React.js component not re-rendering children when the useState hook changes state React.js useState hook 导致太多重新渲染,无法更新我的 state - React.js useState hook causes too many re-renders and cant update my state 反应 useState 钩子 - 更新 object 的 state - React useState hook - update state of a object 添加存储在 useState 挂钩 React.js 中的数组元素的值 - Add value of array elements stored in useState hook React.js react.js 的 useState 钩子被分配了一个值但没有使用,但这没关系 - react.js useState hook is assigned a value but not used, but that is ok react.js 中的 useState - useState in react.js React js useState 钩子。 单击复选框时如何使用其中的数组更新 json 对象的状态 - React js useState hook. How to update state of a json object with an a array in it when a checkbox is clicked
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM