I imported <Routing/>
to Main App, then the error TypeError: Object(...) is not a function
occurred.
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 :
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'. it was mistake. Correct answer: useHistory from 'react-router-dom'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.