gpt4 book ai didi

javascript - 无法在 React 中安装 "useHistory"

转载 作者:行者123 更新时间:2023-12-05 04:21:24 30 4
gpt4 key购买 nike

我正在做一个谷歌克隆(迷你项目),因为我需要从 react-router-dom 导入 useHistory

我已按照以下步骤操作:

第 1 步:npm install --save react-router-dom(我在终端中使用了这个命令)第 2 步:从“react-router-dom”导入 {useHistory}(在我的文件顶部使用它)第 3 步:const history = useHistory()(在我的代码中使用它)

执行此步骤后,出现以下错误:

export 'useHistory' (imported as 'useHistory') was not found in'react-router-dom' (possible exports: AbortedDeferredError, Await,BrowserRouter, Form, HashRouter, Link, MemoryRouter, NavLink,Navigate, NavigationType, Outlet, Route, Router, RouterProvider,Routes, ScrollRestoration, UNSAFE_DataRouterContext,UNSAFE_DataRouterStateContext, UNSAFE_DataStaticRouterContext,UNSAFE_LocationContext, UNSAFE_NavigationContext, UNSAFE_RouteContext,UNSAFE_enhanceManualRouteObjects, createBrowserRouter,createHashRouter, createMemoryRouter, createPath,createRoutesFromChildren, createRoutesFromElements,createSearchParams, defer, generatePath, isRouteErrorResponse, json,matchPath, matchRoutes, parsePath, redirect, renderMatches,resolvePath, unstable_HistoryRouter, useActionData, useAsyncError,useAsyncValue, useFetcher, useFetchers, useFormAction, useHref,useInRouterContext, useLinkClickHandler, useLoaderData, useLocation,useMatch, useMatches, useNavigate, useNavigation, useNavigationType,useOutlet, useOutletContext, useParams, useResolvedPath,useRevalidator, useRouteError, useRouteLoaderData, useRoutes,useSearchParams, useSubmit)

useHistory 似乎不是 react-router-dom 的一部分。

无法在 React 应用中导入 useHistory

最佳答案

You can try following steps:

  • 安装 React 路由器 dom。 npm install --save react-router-dom。
  • >
    Import the history package from react router dom. import { useHistory } from "react-router-dom"
  • >
    Assign the history function to a variable (not necessary but. recommended)
  • 使用 push() 函数在成功登录后重定向用户。

关于javascript - 无法在 React 中安装 "useHistory",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74216151/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com