簡體   English   中英

這兩個箭頭函數有什么區別?

[英]What is the diff between this two arrow functions?

基本上,我不明白為什么第二個箭頭 function 有效,而第一個無效。

    //this one doesnt
    this.setState = () => {
            text: e.target.value,
        };
  
    //this one works
    this.setState(() => ({
            text: e.target.value,
        }));

第一個是賦值,第二個執行執行

第一個將箭頭 function 分配給 this.setState 並且由於存在一個名為 setState 的現有function ,它將被您的箭頭 function 覆蓋。

第二個將箭頭 function 作為參數傳遞給 setState,setState 將調用您剛剛在其執行過程中傳遞的 function。

您可以參考文檔以更好地了解 setState 的作用。

https://reactjs.org/docs/react-component.html#setstate

暫無
暫無

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

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