簡體   English   中英

新的 react useState 鈎子與“this.state”的等價物是什么?

[英]What is the equivalent of 'this.state' with the new react useState hook?

我正在練習新的 React 鈎子,我想知道是否有人在函數組件中的 useState 鈎子方面弄清楚了 console.log(this.state) 的等價物是什么。

問候!

功能組件中沒有“狀態”。

您可以使用“useReducer”來代替狀態對象。 (如果你的狀態是一個復雜的對象)

const [state, dispatch] = useReducer(reducer, initialState);

如果您將 React 應用程序從基於類轉換為鈎子。

尋找這樣的東西

class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      var1: "foo",
      var2:"bar"
    };
  }

在這種情況下 this.state.var1 = "foo" this.state.var2 = "bar" this.state =

{
      var1: "foo",
      var2:"bar"
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM