繁体   English   中英

我导入了`<routing /> `

[英]I imported `<Routing/>`

我将<Routing/>导入到主应用程序,然后出现错误TypeError: Object(...) is not a function

我的应用程序在没有该组件的情况下工作,但我需要修复此错误。 请帮我。

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 />***
    </>
  );
}

基本示例示例Live Demo CodeSandbox

const Routing = () => <h1>Hello from Routing</h1>;

function App() {
  return (
    <>
      <Routing />
    </>
  );
}

export default 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;

我解决了。 我从'react'导入了useHistory。 这是错误的。 正确答案:'react-router-dom' 中的 useHistory

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM