改成自己的router,基本完成网络通信

此提交包含在:
2021-07-26 23:51:30 +08:00
父節點 d8c40cfe59
當前提交 41f5239ae6
共有 9 個檔案被更改,包括 294 行新增23 行删除

查看文件

@@ -0,0 +1,9 @@
.full-window {
width: 100%;
height: 100%;
}
.full-window > div {
background-size: cover !important;
background-position: center !important;
}

查看文件

@@ -0,0 +1,63 @@
import './Frame.css';
import { Component } from 'react';
import History from '../../helper/history';
export class SingleRouter extends Component {
historyRemover = null;
constructor(props) {
super(props);
this.state = {
page: null
};
}
componentDidMount() {
this.historyRemover = History.init(() => {
this.setState({
page: this.getPage()
});
});
this.setState({
page: this.getPage()
});
}
componentWillUnmount() {
this.historyRemover?.();
}
getPage() {
const path = History.getHref();
return this.props.children.filter(component =>
component && (!component.props.path ||
path === component.props.path ||
path.replace(/[\d]*$/, ':id') === component.props.path)
);
}
render() {
return (
<div className="full-window">
{this.state.page}
</div>
);
}
}
export function Route(props) {
return (
<div path={props.path}>
{props.children ?? <props.component />}
</div>
);
}
export function Redirect(props) {
if (History.getHref().match(props.from))
History.force(props.to);
return null;
}
export function Link(props) {
return (<a href={`/#${props.to}`} {...props}>{props.children}</a>);
}