[英]I imported `<Routing/>`
I imported <Routing/>
to Main App, then the error TypeError: Object(...) is not a function
occurred.我将<Routing/>
导入到主应用程序,然后出现错误TypeError: Object(...) is not a function
。
My app is working without the component, but I need fix this error.我的应用程序在没有该组件的情况下工作,但我需要修复此错误。 Please help me.请帮我。
const Routing = () => {
const history = useHistory();
const { state, dispatch } = useContext(UserContext);
useEffect(() => {
const user = JSON.parse(localStorage.getItem("user"));
if (user) {
dispatch({ type: "USER", payload: user });
} else {
if (!history.location.pathname.startsWith("/reset"))
history.push("/signin");
}
}, []);
return ();
};
function App() {
return (
<>
***<Routing />***
</>
);
}
Basic sample example Live Demo CodeSandbox :基本示例示例Live Demo CodeSandbox :
const Routing = () => <h1>Hello from Routing</h1>;
function App() {
return (
<>
<Routing />
</>
);
}
export default App;
Your full code with corrections:您的完整代码更正:
const Routing = () => {
const history = useHistory();
const { state, dispatch } = useContext(UserContext);
useEffect(() => {
const user = JSON.parse(localStorage.getItem("user"));
if (user) {
dispatch({ type: "USER", payload: user });
} else {
if (!history.location.pathname.startsWith("/reset"))
history.push("/signin");
}
}, []);
return <></>;
};
function App() {
return (
<>
<Routing />
</>
);
}
export default App;
I fixed.我解决了。 i imported useHistory from 'react'.我从'react'导入了useHistory。 it was mistake.这是错误的。 Correct answer: useHistory from 'react-router-dom'正确答案:'react-router-dom' 中的 useHistory
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.