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

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

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.

  1. "use client"
  2. import { useState } from 'react';
  3. import { useRouter } from 'next/router';
  4. import Link from 'next/link';
  5. export default function Navbar() {
  6. const [searchValue, setSearchValue] = useState('');
  7. const router = useRouter();
  8. const handleSearch = (e) => {
  9. e.preventDefault();
  10. router.push(`/search/${searchValue}`);
  11. };
  12. const handleInputChange = (e) => {
  13. setSearchValue(e.target.value);
  14. };
  15. return (
  16. <div id="navbarComp">
  17. <div id="navbar">
  18. <Link href="/">
  19. <div id="explore">
  20. <h1>Explore</h1>
  21. </div>
  22. </Link>
  23. </div>
  24. <div id="inputsearch">
  25. <form onSubmit={handleSearch}>
  26. <input
  27. className="dog"
  28. placeholder="Search"
  29. value={searchValue}
  30. onChange={handleInputChange}
  31. />
  32. </form>
  33. </div>
  34. </div>
  35. );
  36. }

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;

  1. &quot;use client&quot;
  2. import { useState } from &#39;react&#39;;
  3. import { useRouter } from &#39;next/router&#39;;
  4. import Link from &#39;next/link&#39;;
  5. export default function Navbar() {
  6. const [searchValue, setSearchValue] = useState(&#39;&#39;);
  7. const router = useRouter();
  8. const handleSearch = (e) =&gt; {
  9. e.preventDefault();
  10. router.push(`/search/${searchValue}`);
  11. };
  12. const handleInputChange = (e) =&gt; {
  13. setSearchValue(e.target.value);
  14. };
  15. return (
  16. &lt;div id=&quot;navbarComp&quot;&gt;
  17. &lt;div id=&quot;navbar&quot;&gt;
  18. &lt;Link href=&quot;/&quot;&gt;
  19. &lt;div id=&quot;explore&quot;&gt;
  20. &lt;h1&gt;Explore&lt;/h1&gt;
  21. &lt;/div&gt;
  22. &lt;/Link&gt;
  23. &lt;/div&gt;
  24. &lt;div id=&quot;inputsearch&quot;&gt;
  25. &lt;form onSubmit={handleSearch}&gt;
  26. &lt;input
  27. className=&quot;dog&quot;
  28. placeholder=&quot;Search&quot;
  29. value={searchValue}
  30. onChange={handleInputChange}
  31. /&gt;
  32. &lt;/form&gt;
  33. &lt;/div&gt;
  34. &lt;/div&gt;
  35. );
  36. }

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

答案1

得分: 0

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

  1. "use client"
  2. import { useState } from 'react';
  3. import { useRouter } from 'next/navigation';
  4. import Link from 'next/link';
  5. export default function Navbar() {
  6. const [searchValue, setSearchValue] = useState('');
  7. const router = useRouter();
  8. const handleInputChange = (e) => {
  9. setSearchValue(e.target.value);
  10. };
  11. const handleSearch = (e) => {
  12. e.preventDefault();
  13. router.push(`/search/${searchValue}`);
  14. };
  15. return (
  16. <div id="navbarComp">
  17. <div id="navbar">
  18. <Link href="/">
  19. <div id="explore">
  20. <h1>Explore</h1>
  21. </div>
  22. </Link>
  23. </div>
  24. <div id="inputsearch">
  25. <form onSubmit={handleSearch}>
  26. <input
  27. className="dog"
  28. placeholder="Search"
  29. value={searchValue}
  30. onChange={handleInputChange}
  31. onSubmit={() => router.push(`/search/${searchValue}`)}
  32. />
  33. </form>
  34. </div>
  35. </div>
  36. );
  37. }

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

英文:

Nevermind, I worked it out.

Here is the working code

  1. &quot;use client&quot;
  2. import { useState } from &#39;react&#39;;
  3. import { useRouter } from &#39;next/navigation&#39;;
  4. import Link from &#39;next/link&#39;;
  5. export default function Navbar() {
  6. const [searchValue, setSearchValue] = useState(&#39;&#39;);
  7. const router = useRouter();
  8. const handleInputChange = (e) =&gt; {
  9. setSearchValue(e.target.value);
  10. };
  11. const handleSearch = (e) =&gt; {
  12. e.preventDefault();
  13. router.push(`/search/${searchValue}`);
  14. };
  15. return (
  16. &lt;div id=&quot;navbarComp&quot;&gt;
  17. &lt;div id=&quot;navbar&quot;&gt;
  18. &lt;Link href=&quot;/&quot;&gt;
  19. &lt;div id=&quot;explore&quot;&gt;
  20. &lt;h1&gt;Explore&lt;/h1&gt;
  21. &lt;/div&gt;
  22. &lt;/Link&gt;
  23. &lt;/div&gt;
  24. &lt;div id=&quot;inputsearch&quot;&gt;
  25. &lt;form onSubmit={handleSearch}&gt;
  26. &lt;input
  27. className=&quot;dog&quot;
  28. placeholder=&quot;Search&quot;
  29. value={searchValue}
  30. onChange={handleInputChange}
  31. onSubmit={() =&gt; router.push(`/search/${searchValue}`)}
  32. /&gt;
  33. &lt;/form&gt;
  34. &lt;/div&gt;
  35. &lt;/div&gt;
  36. );
  37. }

答案2

得分: 0

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

  1. ...
  2. <input
  3. className="dog"
  4. placeholder="搜索"
  5. value={searchValue}
  6. onChange={(e) => handleInputChange(e)}
  7. />
  8. ...
英文:

try to pass the input event object to handleInputChange like:

  1. ...
  2. &lt;input
  3. className=&quot;dog&quot;
  4. placeholder=&quot;Search&quot;
  5. value={searchValue}
  6. onChange={(e)=&gt; handleInputChange(e)}
  7. /&gt;
  8. ...

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:

确定