简体   繁体   English

我不断收到“useNavigate() 只能在<router> component" 运行时出错,我不确定为什么</router>

[英]I keep getting "useNavigate() may be used only in the context of a <Router> component" error when running this and I'm not sure why

import SearchIcon from '@material-ui/icons/Search';
import MicIcon from '@material-ui/icons/Mic';
import {Button} from '@material-ui/core';
import styled from 'styled-components';
//import {Search} from '@material-ui/icons';
import { useState } from 'react';
import { useNavigate } from 'react-router-dom';
import { useStateValue } from '../SearchProvider.js';
import { SET_SEARCH_TERM } from '../types.js';
import React from 'react';

const SearchInput = styled.div`
  display: flex;
  align-items: center;
  border: 1px solid lightgray;
  height 30px;
  padding: 10px 15px;
  width: 500px;
  margin: 0px auto;
  border-radius: 999px;
  input {
    flex: 1;
    padding: 8px 13px;
    font-size: medium;
    border: 0;
    outline: 0;
  }
  .searchicon {
    color: gray;
  }
`;

const SearchButton = styled.div`
  margin-top: 20px;
  display: flex;
  justify-content: center;
  button {
    margin: 5px;
    background: #f8f8f8 !important;
    border: 1px solid white;
    text-transform: inherit;
    &:hover {
      margin: 5px;
      background: #f8f8f8 !important;
      color: #000;
      border: 1px solid #c6c6c6
    }
  }
`;

const Search = ({ hide }) => {
  const [input, setInput] = useState("");
  const navigate = useNavigate();
  const[{}, dispatch] = useStateValue(); 

  const search = e => {
    e.preventDefault();
    dispatch({
      type: SET_SEARCH_TERM,
      term: input
    })
    console.log(input);
    navigate.push("/search");
  };

  return (
    <form onSubmit={search}>
      <SearchInput>
        <SearchIcon className="searchicon" />
        <input value={input} onChange={e => setInput(e.target.value)} />
        <MicIcon />
      </SearchInput>
      {!hide && (
        <SearchButton>
          <Button type="submit" variant="outlined">Google Search</Button>
          <Button variant="outlined">I am feeling lucky</Button>
        </SearchButton>
      )}
    </form>
  )
};

export default Search

I am trying to make a google clone but I am getting errors in my console saying "Uncaught Error: useNavigate() may be used only in the context of a component" and i'm not sure what else to do.我正在尝试制作谷歌克隆,但我的控制台出现错误,提示“未捕获的错误:useNavigate() 可能仅在组件的上下文中使用”,我不确定还能做什么。 Says the error is coming in the "const navigate = useNavigate();"说错误来自“const navigate = useNavigate();” line.线。 I have tried using "Navigate" and "useHistory" as well but those are working either.我也尝试过使用“Navigate”和“useHistory”,但它们都有效。

A react-router or react-router-dom router component provides the routing context that all routes, links, hooks, etc use. react-routerreact-router-dom路由器组件提供所有路由、链接、挂钩等使用的路由上下文。 The Search component needs to be rendered within router so the useNavigate hook has a routing context available to it. Search组件需要在路由器中呈现,以便useNavigate钩子有一个可用的路由上下文。

Example:例子:

import { BrowserRouter, Routes, Route } from 'react-router-dom';
import Search from '../components/Search';

...

const App = () => {
  ...

  return (
    <BrowserRouter> // <-- Provides routing context
      <div>
        ...
        <Search /> // <-- rendered within Router component
      </div>
      <Routes>
        .... app routes ...
      </Routes>

      ...

    </BrowserRouter>
  );
};

Also, just FYI, navigate is a function , not an object like history that was used in previous versions.另外,仅供参考, navigatefunction ,而不是像以前版本中使用的history那样的 object 。 Use navigate("/search");使用navigate("/search"); instead of navigate.push("/search");而不是navigate.push("/search"); to issue an imperative navigation action.发出命令式导航动作。

暂无
暂无

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

相关问题 错误:useNavigate() 只能在 a 的上下文中使用<router>成分</router> - Error: useNavigate() may be used only in the context of a <Router> component (反应路由器 v6)“错误:useNavigate() 只能在<router>特定组件中的组件”错误</router> - (react router v6) "Error: useNavigate() may be used only in the context of a <Router> component" error in hoc component npm 测试:useNavigate() 只能在<router>成分</router> - npm test: useNavigate() may be used only in the context of a <Router> component 我如何解析“ useHref() 只能在上下文中使用<router>组件”当组件已经在里面时<router> ?</router></router> - How can I resolver " useHref() may be used only in the context of a <Router> component" when component is already inside <Router>? 错误:useNavigate() 只能在 a 的上下文中使用<router>组件虽然我的登录页面的路由位于里面<browserrouter></browserrouter></router> - error: useNavigate() may be used only in the context of a <Router> component Although my login page's route is located inside <BrowserRouter> 未捕获的错误:useLocation() 只能在 a 的上下文中使用<router>零件</router> - Uncaught Error: useLocation() may be used only in the context of a <Router> component React js:错误:useLocation()只能在a的上下文中使用<router>成分</router> - React js: Error: useLocation() may be used only in the context of a <Router> component index.tsx:24 未捕获错误:useLocation() 只能在<router>成分</router> - index.tsx:24 Uncaught Error: useLocation() may be used only in the context of a <Router> component 出现错误:req.validatonErrors 不是 function 但我不知道为什么 - Getting error: req.validatonErrors is not a function but I'm not sure why 我已经在使用功能组件,但我不断收到错误消息:只能在功能组件的主体内调用钩子 - I'm already using a functional component and I keep getting error : Hooks can only be called inside the body of a function component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM