【发布时间】:2020-09-06 15:39:09
【问题描述】:
我正在尝试在我的类中设置 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 组件中定义箭头函数的方式正确吗?
谢谢。
【问题讨论】:
-
你不能在类组件中使用 useEffect(或任何钩子)
-
但是我在我的类组件中使用了 componentWillMount。我需要监听路由变化。
-
那不是钩子,是类组件生命周期函数
-
放入类中,检查是否导入react
标签: reactjs arrow-functions use-effect