Javascript 我导入了`<;路由/>`
我将Javascript 我导入了`<;路由/>`,javascript,reactjs,Javascript,Reactjs,我将导入主应用程序,然后出现错误TypeError:Object(…)不是函数 我的应用程序在没有组件的情况下运行,但我需要修复此错误。 请帮帮我 const Routing = () => { const history = useHistory(); const { state, dispatch } = useContext(UserContext); useEffect(() => { const user = JSON.parse(localStora
导入主应用程序,然后出现错误TypeError:Object(…)不是函数
我的应用程序在没有组件的情况下运行,但我需要修复此错误。
请帮帮我
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 />***
</>
);
}
const路由=()=>{
const history=useHistory();
const{state,dispatch}=useContext(UserContext);
useffect(()=>{
const user=JSON.parse(localStorage.getItem(“用户”);
如果(用户){
分派({type:“USER”,payload:USER});
}否则{
如果(!history.location.pathname.startsWith(“/reset”))
历史推送(“/签名”);
}
}, []);
返回();
};
函数App(){
返回(
******
);
}
基本示例示例:
constrouting=()=>Hello from Routing;
函数App(){
返回(
);
}
导出默认应用程序;
带更正的完整代码:
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;
const路由=()=>{
const history=useHistory();
const{state,dispatch}=useContext(UserContext);
useffect(()=>{
const user=JSON.parse(localStorage.getItem(“用户”);
如果(用户){
分派({type:“USER”,payload:USER});
}否则{
如果(!history.location.pathname.startsWith(“/reset”))
历史推送(“/签名”);
}
}, []);
返回;
};
函数App(){
返回(
);
}
导出默认应用程序;
我已修复。我从“react”中导入了useHistory。这是个错误。回答正确:使用“react router dom”中的History您说您导入了它,但您的代码只显示了一个函数。如果您确实导入了它,那么问题很可能是如何进行导入或导出。提供一个。
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;