Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 我导入了`<;路由/>`_Javascript_Reactjs - Fatal编程技术网

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;