refactor(route): tidy "location"、"history" to useLocation、useNavigate

This commit is contained in:
haitao(lj) 2022-09-29 15:00:22 +08:00
parent f3b801efb2
commit 1beb884d26
11 changed files with 20 additions and 26 deletions

View File

@ -50,7 +50,7 @@ const Index: FC<IProps> = ({ type, qid, aid, title }) => {
});
};
useEffect(() => {
if (window.navigator?.canShare?.({ text: '111' })) {
if (window.navigator?.canShare?.({ text: 'can_share' })) {
setSystemShareState(true);
}
}, []);

View File

@ -1,7 +1,7 @@
import { memo, FC } from 'react';
import { ButtonGroup } from 'react-bootstrap';
import { useTranslation } from 'react-i18next';
import { Link } from 'react-router-dom';
import { Link, useLocation } from 'react-router-dom';
interface Props {
count: number;
@ -11,6 +11,7 @@ const Index: FC<Props> = ({ count = 0, order = 'default' }) => {
const { t } = useTranslation('translation', {
keyPrefix: 'question_detail.answers',
});
const location = useLocation();
return (
<div
className="d-flex align-items-center justify-content-between mt-5 mb-3"
@ -20,14 +21,14 @@ const Index: FC<Props> = ({ count = 0, order = 'default' }) => {
</h5>
<ButtonGroup size="sm">
<Link
to={`${window.location.pathname}?order=default`}
to={`${location.pathname}?order=default`}
className={`btn btn-outline-secondary ${
order !== 'updated' ? 'active' : ''
}`}>
{t('score')}
</Link>
<Link
to={`${window.location.pathname}?order=updated`}
to={`${location.pathname}?order=updated`}
className={`btn btn-outline-secondary ${
order === 'updated' ? 'active' : ''
}`}>

View File

@ -1,6 +1,6 @@
import { useEffect, useState } from 'react';
import { Container, Row, Col } from 'react-bootstrap';
import { useParams, useSearchParams } from 'react-router-dom';
import { useParams, useSearchParams, useNavigate } from 'react-router-dom';
import { questionDetail, getAnswers } from '@answer/api';
import { Pagination, PageTitle } from '@answer/components';
@ -25,6 +25,7 @@ import {
import './index.scss';
const Index = () => {
const navigate = useNavigate();
const { qid = '', aid = '' } = useParams();
const [urlSearch] = useSearchParams();
@ -75,7 +76,7 @@ const Index = () => {
const initPage = (type: string) => {
if (type === 'delete_question') {
setTimeout(() => {
window.history.back();
navigate(-1);
}, 1000);
return;
}

View File

@ -111,7 +111,7 @@ const Ask = () => {
id: aid,
};
modifyAnswer(params).then(() => {
window.location.href = `/questions/${qid}/${aid}`;
navigate(`/questions/${qid}/${aid}`);
});
};

View File

@ -1,6 +1,6 @@
import { FC, memo } from 'react';
import { ListGroupItem, ButtonGroup, Button } from 'react-bootstrap';
import { useSearchParams, useNavigate } from 'react-router-dom';
import { useSearchParams, useNavigate, useLocation } from 'react-router-dom';
import { useTranslation } from 'react-i18next';
const sortBtns = [
@ -21,6 +21,7 @@ interface Props {
}
const Index: FC<Props> = ({ sort, count = 0 }) => {
const [searchParams] = useSearchParams();
const location = useLocation();
const navigate = useNavigate();
const { t } = useTranslation('translation', {
@ -28,7 +29,7 @@ const Index: FC<Props> = ({ sort, count = 0 }) => {
});
const handleParams = (order): string => {
const basePath = window.location.pathname;
const basePath = location.pathname;
searchParams.delete('page');
searchParams.set('order', order);
const searchStr = searchParams.toString();

View File

@ -17,7 +17,7 @@ const Index: FC = () => {
activateAccount(encodeURIComponent(code)).then((res) => {
updateUser(res);
setTimeout(() => {
window.location.href = '/users/account-activation/success';
window.location.replace('/users/account-activation/success');
}, 0);
});
}

View File

@ -108,13 +108,9 @@ const Index: React.FC = () => {
setRefresh((pre) => pre + 1);
}
if (res.mail_status === 1) {
const path = Storage.get('ANSWER_PATH');
const path = Storage.get('ANSWER_PATH') || '/';
Storage.remove('ANSWER_PATH');
if (path) {
window.location.href = path;
} else {
window.location.href = '/';
}
window.location.replace(path);
}
setModalState(false);

View File

@ -1,6 +1,6 @@
import { FC, memo } from 'react';
import { ButtonGroup, Button } from 'react-bootstrap';
import { useSearchParams, useNavigate } from 'react-router-dom';
import { useSearchParams, useNavigate, useLocation } from 'react-router-dom';
import { useTranslation } from 'react-i18next';
const sortBtns = [
@ -26,10 +26,11 @@ const Index: FC<Props> = ({
}) => {
const [searchParams] = useSearchParams();
const navigate = useNavigate();
const location = useLocation();
const { t } = useTranslation('translation', { keyPrefix: 'personal' });
const handleParams = (order): string => {
const basePath = window.location.pathname;
const basePath = location.pathname;
searchParams.delete('page');
searchParams.set('order', order);
const searchStr = searchParams.toString();

View File

@ -9,7 +9,7 @@ const Suspended = () => {
const userInfo = userInfoStore((state) => state.user);
if (userInfo.status !== 'forbidden') {
window.location.href = '/';
window.location.replace('/');
return null;
}

View File

@ -34,7 +34,7 @@ function isLogin(needToLogin?: boolean): boolean {
// login and active
if (user.username && user.mail_status === 1) {
if (LOGIN_NEED_BACK.includes(path)) {
window.location.href = '/';
window.location.replace('/');
}
return true;
}

View File

@ -6,12 +6,6 @@ import { userInfoStore, toastStore } from '@answer/stores';
import Storage from './storage';
// type Result<T> = {
// code: number;
// msg: string;
// data: T;
// };
const API = {
development: '',
production: '',