React useEffect 不会在路由更改时触发
React useEffect is not triggering on route change
我希望每次路由更改(从 Component1 切换到 Component2)时运行 console.log('Refresh')
。但它只在第一次渲染时触发。为什么?
index.js
:
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import { BrowserRouter } from 'react-router-dom';
ReactDOM.render(<BrowserRouter><App /></BrowserRouter>, document.getElementById('root'));
App.js
:
import React, { useEffect } from 'react';
import { Switch, Route } from 'react-router-dom';
import Nav from './Nav';
import Component1 from './Component1';
import Component2 from './Component2';
const App = () => {
useEffect( () => console.log('Refresh'));
return (
[<Switch>
<Route component = {Nav}/>
</Switch>,
<Switch>
<Route exact path = '/component1' component = {Component1}/>
<Route exact path = '/component2' component = {Component2}/>
</Switch>]
);
}
export default App;
Nav.js
:
import React from 'react';
import { Link } from 'react-router-dom';
const Nav = () => {
return (
<div>
<Link to = '/component1'>Component 1</Link>
<Link to = '/component2'>Component 2</Link>
</div>
);
}
export default Nav;
Component1.js
:
import React from 'react';
const Component1 = () => {
return (
<div>
<p>Hi</p>
</div>
);
}
export default Component1;
Component2.js
:
import React from 'react';
const Component2 = () => {
return (
<div>
<p>Bye</p>
</div>
);
}
export default Component2;
useEffect
没有被触发,因为 App
组件没有被重新渲染,那个组件没有任何变化(没有状态或道具更新)。
如果你想让 App
组件在路由改变时重新渲染,你可以使用 withRouter
HOC 注入路由属性,像这样:
import { Switch, Route, withRouter } from 'react-router-dom';
const App = () => {
useEffect( () => console.log('Refresh'));
return (...);
}
export default withRouter(App);
使用 key 属性,所以每次我们渲染新组件(不同的键)
<Route path='/mypath/:username' exact render= {routeProps =><MyCompo {...routeProps} key={document.location.href} />} />
使用 useEffect 的第二个参数有条件地应用效果。例如通过 react-router-dom,你得到一些属性
const { schoolId, classId } = props
useEffect(() => {
// fetch something here
}, [schoolId, classId)
这里[schoolId, classId
作为useEffect触发的唯一标识
使用 useLocation
和 useLayoutEffect
获得更高的效率:
import { useLocation } from "react-router-dom";
//...
const location = useLocation();
//...
useLayoutEffect(() => {
console.log("location",location)
}, [location])
我希望每次路由更改(从 Component1 切换到 Component2)时运行 console.log('Refresh')
。但它只在第一次渲染时触发。为什么?
index.js
:
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import { BrowserRouter } from 'react-router-dom';
ReactDOM.render(<BrowserRouter><App /></BrowserRouter>, document.getElementById('root'));
App.js
:
import React, { useEffect } from 'react';
import { Switch, Route } from 'react-router-dom';
import Nav from './Nav';
import Component1 from './Component1';
import Component2 from './Component2';
const App = () => {
useEffect( () => console.log('Refresh'));
return (
[<Switch>
<Route component = {Nav}/>
</Switch>,
<Switch>
<Route exact path = '/component1' component = {Component1}/>
<Route exact path = '/component2' component = {Component2}/>
</Switch>]
);
}
export default App;
Nav.js
:
import React from 'react';
import { Link } from 'react-router-dom';
const Nav = () => {
return (
<div>
<Link to = '/component1'>Component 1</Link>
<Link to = '/component2'>Component 2</Link>
</div>
);
}
export default Nav;
Component1.js
:
import React from 'react';
const Component1 = () => {
return (
<div>
<p>Hi</p>
</div>
);
}
export default Component1;
Component2.js
:
import React from 'react';
const Component2 = () => {
return (
<div>
<p>Bye</p>
</div>
);
}
export default Component2;
useEffect
没有被触发,因为 App
组件没有被重新渲染,那个组件没有任何变化(没有状态或道具更新)。
如果你想让 App
组件在路由改变时重新渲染,你可以使用 withRouter
HOC 注入路由属性,像这样:
import { Switch, Route, withRouter } from 'react-router-dom';
const App = () => {
useEffect( () => console.log('Refresh'));
return (...);
}
export default withRouter(App);
使用 key 属性,所以每次我们渲染新组件(不同的键)
<Route path='/mypath/:username' exact render= {routeProps =><MyCompo {...routeProps} key={document.location.href} />} />
使用 useEffect 的第二个参数有条件地应用效果。例如通过 react-router-dom,你得到一些属性
const { schoolId, classId } = props
useEffect(() => {
// fetch something here
}, [schoolId, classId)
这里[schoolId, classId
作为useEffect触发的唯一标识
使用 useLocation
和 useLayoutEffect
获得更高的效率:
import { useLocation } from "react-router-dom";
//...
const location = useLocation();
//...
useLayoutEffect(() => {
console.log("location",location)
}, [location])