【发布时间】:2020-01-22 17:53:39
【问题描述】:
我希望 useEffect 能够控制日志,但项目无法编译并在终端出现错误 Unexpected use of 'location' no-restricted-globals 我不必使用 useEffect 如果我能在每次位置更改时触发一个函数会很高兴
import React, { useState, useEffect } from 'react';
import { BrowserRouter, Switch, Route } from 'react-router-dom';
import { CSSTransition, TransitionGroup } from 'react-transition-group'
import { Context } from "./components/context";
import { Layout } from "./components/layout";
import { Home } from './routes/home';
import { NotFound } from './routes/not-found';
const Router: React.FC = () => {
useEffect(() => {
console.log('location changed')
}, [location]);
return (
<Context.Provider value={{ global, setGlobal }}>
<BrowserRouter>
<Route render={({location}) => ( //<-------- this is the location!!!
<Layout>
<TransitionGroup>
<CSSTransition timeout={450} classNames='fade' key={location.key}>
<Switch location={location}>
<Route exact path = '/' component = {Home} />
<Route exact path = '/my/profile/' component = {Home} />
<Route component = {NotFound}/>
</Switch>
</CSSTransition>
</TransitionGroup>
</Layout>
)} />
</BrowserRouter>
</Context.Provider>
);
}
export { Router };
【问题讨论】:
-
该问题并非特定于打字稿或功能组件,因此响应都在基于类的组件中显示答案
标签: reactjs typescript react-router-v4