我在 react js 中使用 use effect,如果我在我的 react 應用程式中使用“effect”,它似乎有問題然后它給我一個錯誤,說“意外的令牌”
import React, { Component , useEffect} from 'react'
class Effect extends Component {
useEffect(() => {
console.log('Effect');
})
constructor() {
super()
this.state =
{
count :0
}
this.CH = () =>
{
this.setState(PS =>{
console.log(PS);
return {count:PS.count 1}
})
}
}
render()
{
return(
<>
<button onClick={this.CH}>
</button>
<p>
{`You have clicked me ${this.state.count} times.`}
</p>
</>
)
}
}
export default Effect
這是 useEffect 代碼:
useEffect(() => {
console.log('Effect');
})
所以我關注了一些視頻,但它似乎對我不起作用,他們在基于功能的組件中使用它。但我不是,所以我們的問題只有這個嗎?
uj5u.com熱心網友回復:
您只能在基于函式的組件上使用useEffect!在基于類的情況下,您有一些選擇:componentDidMount()
檔案:https ://reactjs.org/docs/react-component.html#componentdidmount
建議:如果你大部分時間都在創建一個新的 React 應用程式,最好的辦法是使用函陣列件。來源:https ://djoech.medium.com/functional-vs-class-components-in-react-231e3fbd7108#:~:text=The most obvious one difference,which returns a React element .
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/537847.html
上一篇:嘿,我收到Navbar.jsx:33UncaughtTypeError:Cannotreadpropertiesofundefined(reading'Text')atNavbar
