[英]Not rendering with react-router-dom
Before I used react-router-dom and I hadn't any problem and I changed my route without any problem.在我使用 react-router-dom 之前我没有任何问题,我改变了我的路线没有任何问题。
But now I bring hook inside of my project and I got a problem.但是现在我把钩子带进了我的项目,我遇到了一个问题。 When I use
<NavLink>
, my route changes but it does not render anything from my component.当我使用
<NavLink>
时,我的路线会发生变化,但它不会从我的组件中呈现任何内容。 When I refresh my page, the component will appear.当我刷新页面时,该组件将出现。
My App.js:我的 App.js:
import { BrowserRouter as Router, Switch, Route } from "react-router-dom";
const routes={
route: `/main/symbol/:title/:id`,
exact: true,
component: Symbol,
},
{
route: `/main/symbolDetails/:title/:id`,
exact: true,
component: SymbolDetails,
},
render(){
<Router>
<Switch>
{routes.map((route, k) => (
<Route
key={k}
exact={route.exact}
path={route.route}
component={route.component}
/>
))}
</Switch>
</Router>
}
My Home.js:我的主页.js:
(in this component I use navlink for changing my page) (在这个组件中,我使用 navlink 来更改我的页面)
import GridContainer from "../../../components/Grid/GridContainer.js";
import "perfect-scrollbar/css/perfect-scrollbar.css";
// @material-ui/core components
import { makeStyles } from "@material-ui/core/styles";
// core components
import Navbar from "../../../components/Navbars/Navbar.js";
import Sidebar from "../../../components/Sidebar/Sidebar.js";
const useStyles = makeStyles(styles);
export default function Admin({ ...rest }) {
// styles
const classes = useStyles();
const [data, setData] = useState([]);
useEffect(() => getSymbolGroup(), []);
const getSymbolGroup = async () => {
let { data } = await symbolGroup.getSymbolGroup();
setData(data.data);
// console.log("data", data);
};
return (
<div className={classes.wrapper}>
<Sidebar
logoText={"Creative Tim"}
logo={logo}
color={color}
{...rest}
/>
<div className={classes.mainPanel}>
<Navbar
/>
<div className={classes.content}>
<div className={classes.container}>
<GridContainer>
{data &&
data.length &&
data.map((x, key) => {
return (
<div className="Subscrip Bshadow ">
<NavLink
to={`/main/symbol/${x.title}/${x.id}`}
className="a rightanime display awidth flexd"
exact
>
<div className="">
<div className="iconpro display">
<img
className="imgwidth "
src={`http://api.atahlil.com/Core/Download/${x.fileId}`}
/>
</div>
</div>
<div className="">
<p style={{ color: "#a3b0c3", width: "100%" }}>
{x.title}
</p>
</div>
</NavLink>
</div>
);
})}
</GridContainer>
</div>
</div>
)}
I realized my problem.我意识到我的问题。 as I say it was correct when I use in class component.
正如我所说,当我在 class 组件中使用时它是正确的。 it is not correct because of my useEffect (hook).
由于我的 useEffect (钩子),这是不正确的。 I had to use accolade (I mean {}) after use UseEffect in Home.js component.
在 Home.js 组件中使用 UseEffect 后,我不得不使用 accolade(我的意思是 {})。
home.js
主页.js
useEffect(() => getSymbolGroup(), []); //it is not correct and I need to refresh my page to render
and the way I had to use useEffect is:我不得不使用 useEffect 的方式是:
useEffect(() => {
getSymbolGroup();
}, []);
// its correct and does not need to refresh page
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.