[英]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:选项 1 :
State
object:
class User extends React.component {
state = {
username: 'Tyler'
}
Option 2 : useState
hook:选项 2 :
useState
挂钩:
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 解决痛点:
Managing State: Reusing logic between multiple components can lead to wrapper hell or deeply nested components.
管理 State:在多个组件之间重用逻辑可能导致包装地狱或深度嵌套的组件。
Side Effects: Unrelated mixed in logic in lifecycle methods can get repetitive, and cause unnecessary side effects.
副作用:生命周期方法中不相关的逻辑混合可能会重复,并导致不必要的副作用。
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.