发现react-route
用Link
或Push
跳转时,没有刷新页面,可是Url
变了,并且点击浏览器自动的返回按钮,Url
变了可是页面不刷新,怎么作到的呢?因而本妹子就从这个方向研究了下react-route
的源码,给小伙伴们分享下。html
经过location.hash
来达到url
变但页面不刷新react
location.hash=hash
复制代码
而后在经过onhashchange
监听浏览器的返回事件android
window.addEventListener('onhashchange', (event) => {
changeDisplayName();//替换显示的内容
});
复制代码
经过pushState来达到url变但页面不刷新,history.push
实际是用原生history.pushState
来实现的,history.replace
实际是用原生history.replaceState
来实现的。git
changeDisplayName();//替换显示的内容
window.history.pushState(null, null, newUrl);
复制代码
而后在经过popstate
监听浏览器的返回事件github
window.addEventListener('popstate', (event) => {
changeDisplayName();//替换显示的内容
});
复制代码
具体代码为codepen
上的change page with history packagenpm
import React, { useEffect, useState, useRef, Component } from 'react';
const MapPage=()=>{
return <div>MapPage</div>
}
const RankPage=()=>{
return <div>RankPage</div>
}
function ConPage() {
const[Page, setPage] = useState('rank');
useEffect(()=>{
window.addEventListener('popstate', (event) => {
console.log("location: " + document.location + ", state: " + JSON.stringify(event.page));
let val;
if(event.page=='rank') {
val='rank'
}else{
val='map'
}
console.log('useEffect',val)
setPage(val)
});
},[])
const _changePage = () => {
if(Page=='rank') {
setPage('map')
window.history.pushState({page:'map'}, null, 'http://dev.jd.com:10086/con?pId=map');
}else{
setPage('rank')
window.history.pushState({page:'rank'}, null, 'http://dev.jd.com:10086/con?pId=rank');
}
}
return (
<div> <div onClick={_changePage} className='btnTest'> 切换路由</div> {Page=='rank' && <RankPage /> } {Page=='map' && <MapPage /> } </div>
)
}
export default ConPage
复制代码
popstate
和onhashchange
方法对android4.4.4
不兼容,须要引入history这个npm
包,里面有兼容性代码,若是判断不兼容,就直接按照window.location.href
跳转。浏览器
具体代码为codepen
上的change URL with history packagereact-router
const history = History.createBrowserHistory();
const Location = history.location;
const MapPage=()=>{
return <div>MapPage</div>
}
const RankPage=()=>{
return <div>RankPage</div>
}
function ConPage() {
const[Page, setPage] = React.useState('rank');
React.useEffect(()=>{
history.listen((Location, action) => {
console.log(action, Location.state);
if(Location.state.page && Location.state.page=='map'){
setPage('map')
}else{
setPage('rank')
}
});
},[])
const _changePage = () => {
if(Page=='rank') {
history.push('/con?pId=map',{ page: 'map' });
}else{
history.push('/con?pId=rank',{ page: 'rank' });
}
}
return (
<div> <button onClick={_changePage} className='btnTest'> 切换路由</button> {Page=='rank' &&<RankPage />} {Page=='map' &&<MapPage />} </div>
)
}
ReactDOM.render(
<ConPage />, document.getElementById('root'), ) 复制代码
react-router里的RouterContext.jsapp
//TODO:直接用React.createContext也能够
import createContext from "mini-create-react-context";
const createNamedContext = name => {
const context = createContext();
context.displayName = name;
return context;
};
const context = /*#__PURE__*/ createNamedContext("Router");
export default context;
复制代码
Context.displayName解释: context
对象接受一个名为 displayName
的property
,类型为字符串。React DevTools
使用该字符串来标示context
要显示的内容。ide
示例,下述组件在 DevTools
中将显示为 MyDisplayName:
const MyContext = React.createContext(/* some value */);
MyContext.displayName = 'MyDisplayName';
<MyContext.Provider> // "MyDisplayName.Provider" 在 DevTools 中
<MyContext.Consumer> // "MyDisplayName.Consumer" 在 DevTools 中
复制代码
Happy coding .. :)