useHistory: 无法读取未定义的属性 (读取 ‘push’)

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

useHistory: Cannot read properties of undefined (reading 'push')

问题

  1. import React from "react";
  2. import { Route, Switch, HashRouter, useHistory } from "react-router-dom";
  3. import Home from "../pages/home/HomeComponent";
  4. import Splash from "../pages/splash/Splash";
  5. import Education from "../pages/education/EducationComponent";
  6. import Experience from "../pages/experience/Experience";
  7. import Contact from "../pages/contact/ContactComponent";
  8. import Projects from "../pages/projects/Projects";
  9. import HobbiesPage from "../pages/hobbies/Hobbies";
  10. import { settings } from "../portfolio.js";
  11. import GamingPage from "../pages/gaming/gaming";
  12. import AnimePage from "../pages/anime/Anime";
  13. import { recognition } from "../components/speechRecognition/SpeechRecognition";
  14. export default function Main(propss) {
  15. const history = useHistory();
  16. recognition.onresult = (event) => {
  17. const command = event.results[0][0].transcript;
  18. if (command.includes("navigate to") || command.includes("go to")) {
  19. if (command.includes("home") || command.includes("index")) {
  20. history.push("/home");
  21. } else if (command.includes("education") || command.includes("index")) {
  22. history.push("/education");
  23. } else if (command.includes("experience") || command.includes("index")) {
  24. history.push("/experience");
  25. } else if (command.includes("contact") || command.includes("index")) {
  26. history.push("/contact");
  27. }
  28. }
  29. };
  30. recognition.onend = () => {
  31. recognition.start();
  32. };
  33. if (settings.isSplash) {
  34. return (
  35. <div>
  36. <HashRouter basename="/">
  37. <Switch>
  38. <Route
  39. path="/"
  40. exact
  41. render={(props) => (
  42. <Splash
  43. {...props}
  44. theme={propss.theme}
  45. setTheme={propss.setTheme}
  46. />
  47. )}
  48. />
  49. <Route
  50. path="/home"
  51. render={(props) => (
  52. <Home
  53. {...props}
  54. theme={propss.theme}
  55. setTheme={propss.setTheme}
  56. />
  57. )}
  58. />
  59. <Route
  60. path="/experience"
  61. exact
  62. render={(props) => (
  63. <Experience
  64. {...props}
  65. theme={propss.theme}
  66. setTheme={propss.setTheme}
  67. />
  68. )}
  69. />
  70. <Route
  71. path="/education"
  72. render={(props) => (
  73. <Education
  74. {...props}
  75. theme={propss.theme}
  76. setTheme={propss.setTheme}
  77. />
  78. )}
  79. />
  80. <Route
  81. path="/contact"
  82. render={(props) => (
  83. <Contact
  84. {...props}
  85. theme={propss.theme}
  86. setTheme={propss.setTheme}
  87. />
  88. )}
  89. />
  90. <Route
  91. path="/splash"
  92. render={(props) => (
  93. <Splash
  94. {...props}
  95. theme={propss.theme}
  96. setTheme={propss.setTheme}
  97. />
  98. )}
  99. />
  100. <Route
  101. path="/projects"
  102. render={(props) => (
  103. <Projects
  104. {...props}
  105. theme={propss.theme}
  106. setTheme={propss.setTheme}
  107. />
  108. )}
  109. />
  110. <Route
  111. path="/hobbies"
  112. render={(props) => (
  113. <HobbiesPage
  114. {...props}
  115. theme={propss.theme}
  116. setTheme={propss.setTheme}
  117. />
  118. )}
  119. />
  120. <Route
  121. path="/gaming"
  122. render={(props) => (
  123. <GamingPage
  124. {...props}
  125. theme={propss.theme}
  126. setTheme={propss.setTheme}
  127. />
  128. )}
  129. />
  130. <Route
  131. path="/anime"
  132. render={(props) => (
  133. <AnimePage
  134. {...props}
  135. theme={propss.theme}
  136. setTheme={propss.setTheme}
  137. />
  138. )}
  139. />
  140. </Switch>
  141. </HashRouter>
  142. </div>
  143. );
  144. } else {
  145. return (
  146. <div>
  147. <HashRouter basename="/">
  148. <Switch>
  149. <Route
  150. path="/"
  151. exact
  152. render={(props) => (
  153. <Home
  154. {...props}
  155. theme={propss.theme}
  156. setTheme={propss.setTheme}
  157. />
  158. )}
  159. />
  160. <Route
  161. path="/home"
  162. render={(props) => (
  163. <Home
  164. {...props}
  165. theme={propss.theme}
  166. setTheme={propss.setTheme}
  167. />
  168. )}
  169. />
  170. <Route
  171. path="/experience"
  172. exact
  173. render={(props) => (
  174. <Experience
  175. {...props}
  176. theme={propss.theme}
  177. setTheme={propss.setTheme}
  178. />
  179. )}
  180. />
  181. <Route
  182. path="/education"
  183. render={(props) => (
  184. <Education
  185. {...props}
  186. theme={propss.theme}
  187. setTheme={propss.setTheme}
  188. />
  189. )}
  190. />
  191. <Route
  192. path="/contact"
  193. render={(props) => (
  194. <Contact
  195. {...props}
  196. theme={propss.theme}
  197. setTheme={propss.setTheme}
  198. />
  199. )}
  200. />
  201. <Route
  202. path="/projects"
  203. render={(props) => (
  204. <Projects
  205. {...props}
  206. theme={propss.theme}
  207. setTheme={propss.setTheme}
  208. />
  209. )}
  210. />
  211. <Route
  212. path="/hobbies"
  213. render={(props) => (
  214. <HobbiesPage
  215. {...props}
  216. theme={propss.theme}
  217. setTheme={propss.setTheme}
  218. />
  219. )}
  220. />
  221. <Route
  222. path="/gaming"
  223. render={(props) => (
  224. <GamingPage
  225. {...props}
  226. theme={propss.theme}
  227. setTheme={propss.setTheme}
  228. />
  229. )}
  230. />
  231. <Route
  232. path="/anime"
  233. render={(props) => (
  234. <AnimePage
  235. {...props}
  236. theme={propss.theme}
  237. setTheme={propss.setTheme}
  238. />
  239. )}
  240. />
  241. </Switch>
  242. </HashRouter>
  243. </div>
  244. );
  245. }
  246. }
英文:
  1. import React from &quot;react&quot;;
  2. import { Route, Switch, HashRouter, useHistory } from &quot;react-router-dom&quot;;
  3. import Home from &quot;../pages/home/HomeComponent&quot;;
  4. import Splash from &quot;../pages/splash/Splash&quot;;
  5. import Education from &quot;../pages/education/EducationComponent&quot;;
  6. import Experience from &quot;../pages/experience/Experience&quot;;
  7. import Contact from &quot;../pages/contact/ContactComponent&quot;;
  8. import Projects from &quot;../pages/projects/Projects&quot;;
  9. import HobbiesPage from &quot;../pages/hobbies/Hobbies&quot;;
  10. import { settings } from &quot;../portfolio.js&quot;;
  11. import GamingPage from &quot;../pages/gaming/gaming&quot;;
  12. import AnimePage from &quot;../pages/anime/Anime&quot;;
  13. import { recognition } from &quot;../components/speechRecognition/SpeechRecognition&quot;;
  14. export default function Main(propss) {
  15. const history = useHistory();
  16. recognition.onresult = (event) =&gt; {
  17. const command = event.results[0][0].transcript;
  18. if (command.includes(&quot;navigate to&quot;) || command.includes(&quot;go to&quot;)) {
  19. if (command.includes(&quot;home&quot;) || command.includes(&quot;index&quot;)) {
  20. history.push(&quot;/home&quot;);
  21. } else if (command.includes(&quot;education&quot;) || command.includes(&quot;index&quot;)) {
  22. history.push(&quot;/education&quot;);
  23. } else if (command.includes(&quot;experience&quot;) || command.includes(&quot;index&quot;)) {
  24. history.push(&quot;/experience&quot;);
  25. } else if (command.includes(&quot;contact&quot;) || command.includes(&quot;index&quot;)) {
  26. history.push(&quot;/contact&quot;);
  27. }
  28. }
  29. };
  30. recognition.onend = () =&gt; {
  31. recognition.start();
  32. };
  33. if (settings.isSplash) {
  34. return (
  35. &lt;div&gt;
  36. &lt;HashRouter basename=&quot;/&quot;&gt;
  37. &lt;Switch&gt;
  38. &lt;Route
  39. path=&quot;/&quot;
  40. exact
  41. render={(props) =&gt; (
  42. &lt;Splash
  43. {...props}
  44. theme={propss.theme}
  45. setTheme={propss.setTheme}
  46. /&gt;
  47. )}
  48. /&gt;
  49. &lt;Route
  50. path=&quot;/home&quot;
  51. render={(props) =&gt; (
  52. &lt;Home
  53. {...props}
  54. theme={propss.theme}
  55. setTheme={propss.setTheme}
  56. /&gt;
  57. )}
  58. /&gt;
  59. &lt;Route
  60. path=&quot;/experience&quot;
  61. exact
  62. render={(props) =&gt; (
  63. &lt;Experience
  64. {...props}
  65. theme={propss.theme}
  66. setTheme={propss.setTheme}
  67. /&gt;
  68. )}
  69. /&gt;
  70. &lt;Route
  71. path=&quot;/education&quot;
  72. render={(props) =&gt; (
  73. &lt;Education
  74. {...props}
  75. theme={propss.theme}
  76. setTheme={propss.setTheme}
  77. /&gt;
  78. )}
  79. /&gt;
  80. &lt;Route
  81. path=&quot;/contact&quot;
  82. render={(props) =&gt; (
  83. &lt;Contact
  84. {...props}
  85. theme={propss.theme}
  86. setTheme={propss.setTheme}
  87. /&gt;
  88. )}
  89. /&gt;
  90. &lt;Route
  91. path=&quot;/splash&quot;
  92. render={(props) =&gt; (
  93. &lt;Splash
  94. {...props}
  95. theme={propss.theme}
  96. setTheme={propss.setTheme}
  97. /&gt;
  98. )}
  99. /&gt;
  100. &lt;Route
  101. path=&quot;/projects&quot;
  102. render={(props) =&gt; (
  103. &lt;Projects
  104. {...props}
  105. theme={propss.theme}
  106. setTheme={propss.setTheme}
  107. /&gt;
  108. )}
  109. /&gt;
  110. &lt;Route
  111. path=&quot;/hobbies&quot;
  112. render={(props) =&gt; (
  113. &lt;HobbiesPage
  114. {...props}
  115. theme={propss.theme}
  116. setTheme={propss.setTheme}
  117. /&gt;
  118. )}
  119. /&gt;
  120. &lt;Route
  121. path=&quot;/gaming&quot;
  122. render={(props) =&gt; (
  123. &lt;GamingPage
  124. {...props}
  125. theme={propss.theme}
  126. setTheme={propss.setTheme}
  127. /&gt;
  128. )}
  129. /&gt;
  130. &lt;Route
  131. path=&quot;/anime&quot;
  132. render={(props) =&gt; (
  133. &lt;AnimePage
  134. {...props}
  135. theme={propss.theme}
  136. setTheme={propss.setTheme}
  137. /&gt;
  138. )}
  139. /&gt;
  140. &lt;/Switch&gt;
  141. &lt;/HashRouter&gt;
  142. &lt;/div&gt;
  143. );
  144. } else {
  145. return (
  146. &lt;div&gt;
  147. &lt;HashRouter basename=&quot;/&quot;&gt;
  148. &lt;Switch&gt;
  149. &lt;Route
  150. path=&quot;/&quot;
  151. exact
  152. render={(props) =&gt; (
  153. &lt;Home
  154. {...props}
  155. theme={propss.theme}
  156. setTheme={propss.setTheme}
  157. /&gt;
  158. )}
  159. /&gt;
  160. &lt;Route
  161. path=&quot;/home&quot;
  162. render={(props) =&gt; (
  163. &lt;Home
  164. {...props}
  165. theme={propss.theme}
  166. setTheme={propss.setTheme}
  167. /&gt;
  168. )}
  169. /&gt;
  170. &lt;Route
  171. path=&quot;/experience&quot;
  172. exact
  173. render={(props) =&gt; (
  174. &lt;Experience
  175. {...props}
  176. theme={propss.theme}
  177. setTheme={propss.setTheme}
  178. /&gt;
  179. )}
  180. /&gt;
  181. &lt;Route
  182. path=&quot;/education&quot;
  183. render={(props) =&gt; (
  184. &lt;Education
  185. {...props}
  186. theme={propss.theme}
  187. setTheme={propss.setTheme}
  188. /&gt;
  189. )}
  190. /&gt;
  191. &lt;Route
  192. path=&quot;/contact&quot;
  193. render={(props) =&gt; (
  194. &lt;Contact
  195. {...props}
  196. theme={propss.theme}
  197. setTheme={propss.setTheme}
  198. /&gt;
  199. )}
  200. /&gt;
  201. &lt;Route
  202. path=&quot;/projects&quot;
  203. render={(props) =&gt; (
  204. &lt;Projects
  205. {...props}
  206. theme={propss.theme}
  207. setTheme={propss.setTheme}
  208. /&gt;
  209. )}
  210. /&gt;
  211. &lt;Route
  212. path=&quot;/hobbies&quot;
  213. render={(props) =&gt; (
  214. &lt;HobbiesPage
  215. {...props}
  216. theme={propss.theme}
  217. setTheme={propss.setTheme}
  218. /&gt;
  219. )}
  220. /&gt;
  221. &lt;Route
  222. path=&quot;/gaming&quot;
  223. render={(props) =&gt; (
  224. &lt;GamingPage
  225. {...props}
  226. theme={propss.theme}
  227. setTheme={propss.setTheme}
  228. /&gt;
  229. )}
  230. /&gt;
  231. &lt;Route
  232. path=&quot;/anime&quot;
  233. render={(props) =&gt; (
  234. &lt;AnimePage
  235. {...props}
  236. theme={propss.theme}
  237. setTheme={propss.setTheme}
  238. /&gt;
  239. )}
  240. /&gt;
  241. &lt;/Switch&gt;
  242. &lt;/HashRouter&gt;
  243. &lt;/div&gt;
  244. );
  245. }
  246. }

> Uncaught TypeError: Cannot read properties of undefined (reading
> 'push')
> at Main.components_speechRecognition_SpeechRecognition__WEBPACK_IMPORTED_MODULE_12_.recognition.onresult
> (Main.js:22:1)
> Main.components_speechRecognition_SpeechRecognition__WEBPACK_IMPORTED_MODULE_12_.recognition.onresult
> @ Main.js:22

And

> VM338:2 Uncaught ReferenceError: process is not defined
> at 4043 (<anonymous>:2:13168)
> at r (<anonymous>:2:306599)
> at 8048 (<anonymous>:2:9496)
> at r (<anonymous>:2:306599)
> at 8641 (<anonymous>:2:1379)
> at r (<anonymous>:2:306599)
> at <anonymous>:2:315627
> at <anonymous>:2:324225
> at <anonymous>:2:324229
> at e.onload (index.js:1:1)

答案1

得分: 1

The useHistory hook needs a routing context provided above it in the ReactTree. Move the HashRouter to wrap this Main component so it has a defined history reference to use.

示例:

  1. import { HashRouter } from "react-router-dom";
  2. ...
  3. <HashRouter>
  4. ...
  5. <Main />
  6. ...
  7. </HashRouter>
  1. import React from "react";
  2. import { Route, Switch, useHistory } from "react-router-dom";
  3. ...
  4. export default function Main(propss) {
  5. const history = useHistory();
  6. recognition.onresult = (event) => {
  7. const command = event.results[0][0].transcript;
  8. if (command.includes("navigate to") || command.includes("go to")) {
  9. if (command.includes("home") || command.includes("index")) {
  10. history.push("/home");
  11. } else if (command.includes("education") || command.includes("index")) {
  12. history.push("/education");
  13. } else if (command.includes("experience") || command.includes("index")) {
  14. history.push("/experience");
  15. } else if (command.includes("contact") || command.includes("index")) {
  16. history.push("/contact");
  17. }
  18. }
  19. };
  20. recognition.onend = () => {
  21. recognition.start();
  22. };
  23. if (settings.isSplash) {
  24. return (
  25. <div>
  26. <Switch>
  27. ...
  28. </Switch>
  29. </div>
  30. );
  31. } else {
  32. return (
  33. <div>
  34. <Switch>
  35. ...
  36. </Switch>
  37. </div>
  38. );
  39. }
  40. }
英文:

The useHistory hook needs a routing context provided above it in the ReactTree. Move the HashRouter to wrap this Main component so it has a defined history reference to use.

Example:

  1. import { HashRouter } from &quot;react-router-dom&quot;;
  2. ...
  3. &lt;HashRouter&gt;
  4. ...
  5. &lt;Main /&gt;
  6. ...
  7. &lt;/HashRouter&gt;
  1. import React from &quot;react&quot;;
  2. import { Route, Switch, useHistory } from &quot;react-router-dom&quot;;
  3. ...
  4. export default function Main(propss) {
  5. const history = useHistory();
  6. recognition.onresult = (event) =&gt; {
  7. const command = event.results[0][0].transcript;
  8. if (command.includes(&quot;navigate to&quot;) || command.includes(&quot;go to&quot;)) {
  9. if (command.includes(&quot;home&quot;) || command.includes(&quot;index&quot;)) {
  10. history.push(&quot;/home&quot;);
  11. } else if (command.includes(&quot;education&quot;) || command.includes(&quot;index&quot;)) {
  12. history.push(&quot;/education&quot;);
  13. } else if (command.includes(&quot;experience&quot;) || command.includes(&quot;index&quot;)) {
  14. history.push(&quot;/experience&quot;);
  15. } else if (command.includes(&quot;contact&quot;) || command.includes(&quot;index&quot;)) {
  16. history.push(&quot;/contact&quot;);
  17. }
  18. }
  19. };
  20. recognition.onend = () =&gt; {
  21. recognition.start();
  22. };
  23. if (settings.isSplash) {
  24. return (
  25. &lt;div&gt;
  26. &lt;Switch&gt;
  27. ...
  28. &lt;/Switch&gt;
  29. &lt;/div&gt;
  30. );
  31. } else {
  32. return (
  33. &lt;div&gt;
  34. &lt;Switch&gt;
  35. ...
  36. &lt;/Switch&gt;
  37. &lt;/div&gt;
  38. );
  39. }
  40. }

huangapple
  • 本文由 发表于 2023年5月26日 08:53:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76337034.html
匿名

发表评论

匿名网友

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

确定