【发布时间】:2021-02-08 13:13:16
【问题描述】:
我遇到了 react history.push 的问题。我的代码的逻辑是,当 API 调用完成时,如果响应的登录状态为 false,我想推送到另一个路由。 URL 正在更改,但在我刷新页面之前组件不会呈现。
这是我更改路线的代码
useEffect(() => {
const getProductsData = async () => {
setIsLoading(true);
const productsData = await fetchProductsApi();
setIsLogin(productsData.data.login);
setProductsArray(productsData.data.data);
setIsLoading(false);
console.log(productsData.data);
if (productsData.data.login === false) {
history.push("/");
}
};
getProductsData();
}, [stock]);
这是我的路线代码
const AuthenticatedRoutes = () => {
return (
<Router>
<BottomNavigationMenu />
<Switch>
<Route path="/add_product" component={AddNewProduct} />
<Route path="/add_image/:id" component={AddImage} />
<Route path="/products" component={Products} />
<Route path="/dashboard" component={Dashboard} />
</Switch>
</Router>
);
};
return (
<Router>
<Switch>
<Route exact path="/" component={Store} />
<Route path="/product_detail" component={ProductDetail} />
<Route path="/signup" component={Signup} />
<Route exact path="/signin" component={SignIn} />
<Route component={AuthenticatedRoutes} />
</Switch>
</Router>
这是我的商店组件
import React from 'react';
import styles from "./css/store.module.css"
import SearchIcon from '@material-ui/icons/Search';
const Store = () => {
return (
<div className={styles.container}>
<div className={styles.store_card}>
<h1 className={styles.store_name}>Fliq shop</h1>
<h1 className={styles.store_location}>Location</h1>
</div>
</div>
);
}
export default Store;
【问题讨论】:
-
你能展示你的商店组件吗?
-
我将编辑问题并向其添加商店组件@Yoel
-
好的,看看我的回答
-
能不能把安慰
history的结果显示一下
标签: reactjs routes react-router react-router-dom history