Next.js 显示 ‘NextRouter 未挂载’ 错误。

huangapple go评论96阅读模式
英文:

Nextjs showing 'NextRouter was not mounted' error

问题

In this code, you are attempting to retrieve the value of an input and redirect to '/search/${searchvalue}', where 'searchvalue' represents the input's value. However, you encounter an error message: 'Error: NextRouter was not mounted'. You can find more information about this error here.

"use client"
import { useState } from 'react';
import { useRouter } from 'next/router';
import Link from 'next/link';

export default function Navbar() {
  const [searchValue, setSearchValue] = useState('');
  const router = useRouter();

  const handleSearch = (e) => {
    e.preventDefault();
    router.push(`/search/${searchValue}`);
  };

  const handleInputChange = (e) => {
    setSearchValue(e.target.value);
  };

  return (
    <div id="navbarComp">
      <div id="navbar">
        <Link href="/">
          <div id="explore">
            <h1>Explore</h1>
          </div>
        </Link>
      </div>
      <div id="inputsearch">
        <form onSubmit={handleSearch}>
          <input
            className="dog"
            placeholder="Search"
            value={searchValue}
            onChange={handleInputChange}
          />
        </form>
      </div>
    </div>
  );
}

You've tried multiple alternatives, but none of them appeared to work.

英文:

In this code, I am trying to get the value of input and trying to redirect it to &#39;/search/${searchvalue}&#39; where &#39;searchvalue&#39; is the value of the input. However whenever I try to do so, I get an error saying &#39;Error: NextRouter was not mounted. https://nextjs.org/docs/messages/next-router-not-mounted&#39;

&quot;use client&quot;
import { useState } from &#39;react&#39;;
import { useRouter } from &#39;next/router&#39;;
import Link from &#39;next/link&#39;;

export default function Navbar() {
  const [searchValue, setSearchValue] = useState(&#39;&#39;);
  const router = useRouter();

  const handleSearch = (e) =&gt; {
    e.preventDefault();
    router.push(`/search/${searchValue}`);
  };

  const handleInputChange = (e) =&gt; {
    setSearchValue(e.target.value);
  };

  return (
    &lt;div id=&quot;navbarComp&quot;&gt;
      &lt;div id=&quot;navbar&quot;&gt;
        &lt;Link href=&quot;/&quot;&gt;
          &lt;div id=&quot;explore&quot;&gt;
            &lt;h1&gt;Explore&lt;/h1&gt;
          &lt;/div&gt;
        &lt;/Link&gt;
      &lt;/div&gt;
      &lt;div id=&quot;inputsearch&quot;&gt;
        &lt;form onSubmit={handleSearch}&gt;
          &lt;input
            className=&quot;dog&quot;
            placeholder=&quot;Search&quot;
            value={searchValue}
            onChange={handleInputChange}
          /&gt;
        &lt;/form&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  );
}

I tried multiple alternatives, none of which seemed to work.

答案1

得分: 0

以下是翻译好的代码部分:

"use client"
import { useState } from 'react';
import { useRouter } from 'next/navigation';
import Link from 'next/link';

export default function Navbar() {
  const [searchValue, setSearchValue] = useState('');
  const router = useRouter();

  const handleInputChange = (e) => {
    setSearchValue(e.target.value);
  };

  const handleSearch = (e) => {
    e.preventDefault();
    router.push(`/search/${searchValue}`);
  };

  return (
    <div id="navbarComp">
      <div id="navbar">
        <Link href="/">
          <div id="explore">
            <h1>Explore</h1>
          </div>
        </Link>
      </div>
      <div id="inputsearch">
        <form onSubmit={handleSearch}>
          <input
            className="dog"
            placeholder="Search"
            value={searchValue}
            onChange={handleInputChange}
            onSubmit={() => router.push(`/search/${searchValue}`)}
          />
        </form>
      </div>
    </div>
  );
}

请注意,代码中的HTML和JavaScript部分已被翻译,但HTML中的特殊字符(如&quot;&lt;)未被翻译,因为它们是用于表示HTML实体的编码,需要保留原样。

英文:

Nevermind, I worked it out.

Here is the working code

&quot;use client&quot;
import { useState } from &#39;react&#39;;
import { useRouter } from &#39;next/navigation&#39;;
import Link from &#39;next/link&#39;;

export default function Navbar() {
  const [searchValue, setSearchValue] = useState(&#39;&#39;);
  const router = useRouter();

  const handleInputChange = (e) =&gt; {
    setSearchValue(e.target.value);
  };

  const handleSearch = (e) =&gt; {
    e.preventDefault();
    router.push(`/search/${searchValue}`);
  };

  return (
    &lt;div id=&quot;navbarComp&quot;&gt;
      &lt;div id=&quot;navbar&quot;&gt;
        &lt;Link href=&quot;/&quot;&gt;
          &lt;div id=&quot;explore&quot;&gt;
            &lt;h1&gt;Explore&lt;/h1&gt;
          &lt;/div&gt;
        &lt;/Link&gt;
      &lt;/div&gt;
      &lt;div id=&quot;inputsearch&quot;&gt;
        &lt;form onSubmit={handleSearch}&gt;
          &lt;input
            className=&quot;dog&quot;
            placeholder=&quot;Search&quot;
            value={searchValue}
            onChange={handleInputChange}
            onSubmit={() =&gt; router.push(`/search/${searchValue}`)}
          /&gt;
        &lt;/form&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  );
}

答案2

得分: 0

尝试将输入事件对象传递给handleInputChange函数,就像这样:

...

<input
 className="dog"
 placeholder="搜索"
 value={searchValue}
 onChange={(e) => handleInputChange(e)}
/>

...
英文:

try to pass the input event object to handleInputChange like:

...

&lt;input
 className=&quot;dog&quot;
 placeholder=&quot;Search&quot;
 value={searchValue}
 onChange={(e)=&gt; handleInputChange(e)}
/&gt;

...

huangapple
  • 本文由 发表于 2023年6月25日 16:57:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76549642.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定