我正在尝试在我的类中设置useEffect挂钩(用于监听路由更改),该类的定义类似于-
export default class AppManger extends Component{
//constructor
//componentWillMount
//reneder
//...
}
其余的钩子已定义并按预期工作,但是当我尝试定义useEffect
-
useEffect(() => {
const { pathname } = location;
console.log('New path:', pathname);
}, [location.pathname]);
我得到-./src/components/AppManger.js
Line 30: Parsing error: Unexpected token
28 | }
29 | }
> 30 | useEffect(() => {
| ^
31 | const { pathname } = location;
32 | console.log('New path:', pathname);
33 | }, [location.pathname]);
在React组件中定义箭头功能是否正确?
谢谢。
import React from "react";
export default AppManger = () => {
useEffect(() => {
const { pathname } = location;
console.log('New path:', pathname);
}, [location.pathname]);
}