1. 程式人生 > >react中裝飾器只能放在元件之後

react中裝飾器只能放在元件之後

@connect(
    state=>state.authority,
    {logout}
)

function Er(){
    return <h2>這是Er</h2>
}

function Three(){
    return <h2>這是Three</h2>
}

Failed to compile

./src/dashbord.js
Syntax error: e:\專案資料夾\react練習\reacthello\hello\my-app\src\dashbord.js: Leading decorators must be attached to a class declaration (15:0)

  13 | )
  14 | 
> 15 | function Er(){
     | ^
  16 |     return <h2>這是Er</h2>
  17 | }
  18 | 

This error occurred during the build time and cannot be dismissed,此時頁面報錯無法渲染元件

當將

function Er(){
    return <h2>這是Er</h2>
}

function Three(){
    return <h2>這是Three</h2>
}

@connect(
    state=>state.authority,
    {logout} 
)

修飾器放在後面時頁面不再報錯。

 

裝飾器的作用:

   當使用裝飾器後將狀態引入到此元件中。