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

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

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

问题

import React from "react";
import { Route, Switch, HashRouter, useHistory } from "react-router-dom";
import Home from "../pages/home/HomeComponent";
import Splash from "../pages/splash/Splash";
import Education from "../pages/education/EducationComponent";
import Experience from "../pages/experience/Experience";
import Contact from "../pages/contact/ContactComponent";
import Projects from "../pages/projects/Projects";
import HobbiesPage from "../pages/hobbies/Hobbies";
import { settings } from "../portfolio.js";
import GamingPage from "../pages/gaming/gaming";
import AnimePage from "../pages/anime/Anime";
import { recognition } from "../components/speechRecognition/SpeechRecognition";

export default function Main(propss) {
  const history = useHistory();
  recognition.onresult = (event) => {
    const command = event.results[0][0].transcript;

    if (command.includes("navigate to") || command.includes("go to")) {
      if (command.includes("home") || command.includes("index")) {
        history.push("/home");
      } else if (command.includes("education") || command.includes("index")) {
        history.push("/education");
      } else if (command.includes("experience") || command.includes("index")) {
        history.push("/experience");
      } else if (command.includes("contact") || command.includes("index")) {
        history.push("/contact");
      }
    }
  };

  recognition.onend = () => {
    recognition.start();
  };

  if (settings.isSplash) {
    return (
      <div>
        <HashRouter basename="/">
          <Switch>
            <Route
              path="/"
              exact
              render={(props) => (
                <Splash
                  {...props}
                  theme={propss.theme}
                  setTheme={propss.setTheme}
                />
              )}
            />
            <Route
              path="/home"
              render={(props) => (
                <Home
                  {...props}
                  theme={propss.theme}
                  setTheme={propss.setTheme}
                />
              )}
            />
            <Route
              path="/experience"
              exact
              render={(props) => (
                <Experience
                  {...props}
                  theme={propss.theme}
                  setTheme={propss.setTheme}
                />
              )}
            />
            <Route
              path="/education"
              render={(props) => (
                <Education
                  {...props}
                  theme={propss.theme}
                  setTheme={propss.setTheme}
                />
              )}
            />
            <Route
              path="/contact"
              render={(props) => (
                <Contact
                  {...props}
                  theme={propss.theme}
                  setTheme={propss.setTheme}
                />
              )}
            />
            <Route
              path="/splash"
              render={(props) => (
                <Splash
                  {...props}
                  theme={propss.theme}
                  setTheme={propss.setTheme}
                />
              )}
            />
            <Route
              path="/projects"
              render={(props) => (
                <Projects
                  {...props}
                  theme={propss.theme}
                  setTheme={propss.setTheme}
                />
              )}
            />
            <Route
              path="/hobbies"
              render={(props) => (
                <HobbiesPage
                  {...props}
                  theme={propss.theme}
                  setTheme={propss.setTheme}
                />
              )}
            />
            <Route
              path="/gaming"
              render={(props) => (
                <GamingPage
                  {...props}
                  theme={propss.theme}
                  setTheme={propss.setTheme}
                />
              )}
            />
            <Route
              path="/anime"
              render={(props) => (
                <AnimePage
                  {...props}
                  theme={propss.theme}
                  setTheme={propss.setTheme}
                />
              )}
            />
          </Switch>
        </HashRouter>
      </div>
    );
  } else {
    return (
      <div>
        <HashRouter basename="/">
          <Switch>
            <Route
              path="/"
              exact
              render={(props) => (
                <Home
                  {...props}
                  theme={propss.theme}
                  setTheme={propss.setTheme}
                />
              )}
            />
            <Route
              path="/home"
              render={(props) => (
                <Home
                  {...props}
                  theme={propss.theme}
                  setTheme={propss.setTheme}
                />
              )}
            />
            <Route
              path="/experience"
              exact
              render={(props) => (
                <Experience
                  {...props}
                  theme={propss.theme}
                  setTheme={propss.setTheme}
                />
              )}
            />
            <Route
              path="/education"
              render={(props) => (
                <Education
                  {...props}
                  theme={propss.theme}
                  setTheme={propss.setTheme}
                />
              )}
            />
            <Route
              path="/contact"
              render={(props) => (
                <Contact
                  {...props}
                  theme={propss.theme}
                  setTheme={propss.setTheme}
                />
              )}
            />
            <Route
              path="/projects"
              render={(props) => (
                <Projects
                  {...props}
                  theme={propss.theme}
                  setTheme={propss.setTheme}
                />
              )}
            />
            <Route
              path="/hobbies"
              render={(props) => (
                <HobbiesPage
                  {...props}
                  theme={propss.theme}
                  setTheme={propss.setTheme}
                />
              )}
            />
            <Route
              path="/gaming"
              render={(props) => (
                <GamingPage
                  {...props}
                  theme={propss.theme}
                  setTheme={propss.setTheme}
                />
              )}
            />
            <Route
              path="/anime"
              render={(props) => (
                <AnimePage
                  {...props}
                  theme={propss.theme}
                  setTheme={propss.setTheme}
                />
              )}
            />
          </Switch>
        </HashRouter>
      </div>
    );
  }
}
英文:
import React from &quot;react&quot;;
import { Route, Switch, HashRouter, useHistory } from &quot;react-router-dom&quot;;
import Home from &quot;../pages/home/HomeComponent&quot;;
import Splash from &quot;../pages/splash/Splash&quot;;
import Education from &quot;../pages/education/EducationComponent&quot;;
import Experience from &quot;../pages/experience/Experience&quot;;
import Contact from &quot;../pages/contact/ContactComponent&quot;;
import Projects from &quot;../pages/projects/Projects&quot;;
import HobbiesPage from &quot;../pages/hobbies/Hobbies&quot;;
import { settings } from &quot;../portfolio.js&quot;;
import GamingPage from &quot;../pages/gaming/gaming&quot;;
import AnimePage from &quot;../pages/anime/Anime&quot;;
import { recognition } from &quot;../components/speechRecognition/SpeechRecognition&quot;;

export default function Main(propss) {
  const history = useHistory();
  recognition.onresult = (event) =&gt; {
    const command = event.results[0][0].transcript;

    if (command.includes(&quot;navigate to&quot;) || command.includes(&quot;go to&quot;)) {
      if (command.includes(&quot;home&quot;) || command.includes(&quot;index&quot;)) {
        history.push(&quot;/home&quot;);
      } else if (command.includes(&quot;education&quot;) || command.includes(&quot;index&quot;)) {
        history.push(&quot;/education&quot;);
      } else if (command.includes(&quot;experience&quot;) || command.includes(&quot;index&quot;)) {
        history.push(&quot;/experience&quot;);
      } else if (command.includes(&quot;contact&quot;) || command.includes(&quot;index&quot;)) {
        history.push(&quot;/contact&quot;);
      }
    }
  };

  recognition.onend = () =&gt; {
    recognition.start();
  };

  if (settings.isSplash) {
    return (
      &lt;div&gt;
        &lt;HashRouter basename=&quot;/&quot;&gt;
          &lt;Switch&gt;
            &lt;Route
              path=&quot;/&quot;
              exact
              render={(props) =&gt; (
                &lt;Splash
                  {...props}
                  theme={propss.theme}
                  setTheme={propss.setTheme}
                /&gt;
              )}
            /&gt;
            &lt;Route
              path=&quot;/home&quot;
              render={(props) =&gt; (
                &lt;Home
                  {...props}
                  theme={propss.theme}
                  setTheme={propss.setTheme}
                /&gt;
              )}
            /&gt;
            &lt;Route
              path=&quot;/experience&quot;
              exact
              render={(props) =&gt; (
                &lt;Experience
                  {...props}
                  theme={propss.theme}
                  setTheme={propss.setTheme}
                /&gt;
              )}
            /&gt;
            &lt;Route
              path=&quot;/education&quot;
              render={(props) =&gt; (
                &lt;Education
                  {...props}
                  theme={propss.theme}
                  setTheme={propss.setTheme}
                /&gt;
              )}
            /&gt;
            &lt;Route
              path=&quot;/contact&quot;
              render={(props) =&gt; (
                &lt;Contact
                  {...props}
                  theme={propss.theme}
                  setTheme={propss.setTheme}
                /&gt;
              )}
            /&gt;
            &lt;Route
              path=&quot;/splash&quot;
              render={(props) =&gt; (
                &lt;Splash
                  {...props}
                  theme={propss.theme}
                  setTheme={propss.setTheme}
                /&gt;
              )}
            /&gt;
            &lt;Route
              path=&quot;/projects&quot;
              render={(props) =&gt; (
                &lt;Projects
                  {...props}
                  theme={propss.theme}
                  setTheme={propss.setTheme}
                /&gt;
              )}
            /&gt;
            &lt;Route
              path=&quot;/hobbies&quot;
              render={(props) =&gt; (
                &lt;HobbiesPage
                  {...props}
                  theme={propss.theme}
                  setTheme={propss.setTheme}
                /&gt;
              )}
            /&gt;
            &lt;Route
              path=&quot;/gaming&quot;
              render={(props) =&gt; (
                &lt;GamingPage
                  {...props}
                  theme={propss.theme}
                  setTheme={propss.setTheme}
                /&gt;
              )}
            /&gt;
            &lt;Route
              path=&quot;/anime&quot;
              render={(props) =&gt; (
                &lt;AnimePage
                  {...props}
                  theme={propss.theme}
                  setTheme={propss.setTheme}
                /&gt;
              )}
            /&gt;
          &lt;/Switch&gt;
        &lt;/HashRouter&gt;
      &lt;/div&gt;
    );
  } else {
    return (
      &lt;div&gt;
        &lt;HashRouter basename=&quot;/&quot;&gt;
          &lt;Switch&gt;
            &lt;Route
              path=&quot;/&quot;
              exact
              render={(props) =&gt; (
                &lt;Home
                  {...props}
                  theme={propss.theme}
                  setTheme={propss.setTheme}
                /&gt;
              )}
            /&gt;
            &lt;Route
              path=&quot;/home&quot;
              render={(props) =&gt; (
                &lt;Home
                  {...props}
                  theme={propss.theme}
                  setTheme={propss.setTheme}
                /&gt;
              )}
            /&gt;
            &lt;Route
              path=&quot;/experience&quot;
              exact
              render={(props) =&gt; (
                &lt;Experience
                  {...props}
                  theme={propss.theme}
                  setTheme={propss.setTheme}
                /&gt;
              )}
            /&gt;
            &lt;Route
              path=&quot;/education&quot;
              render={(props) =&gt; (
                &lt;Education
                  {...props}
                  theme={propss.theme}
                  setTheme={propss.setTheme}
                /&gt;
              )}
            /&gt;
            &lt;Route
              path=&quot;/contact&quot;
              render={(props) =&gt; (
                &lt;Contact
                  {...props}
                  theme={propss.theme}
                  setTheme={propss.setTheme}
                /&gt;
              )}
            /&gt;
            &lt;Route
              path=&quot;/projects&quot;
              render={(props) =&gt; (
                &lt;Projects
                  {...props}
                  theme={propss.theme}
                  setTheme={propss.setTheme}
                /&gt;
              )}
            /&gt;
            &lt;Route
              path=&quot;/hobbies&quot;
              render={(props) =&gt; (
                &lt;HobbiesPage
                  {...props}
                  theme={propss.theme}
                  setTheme={propss.setTheme}
                /&gt;
              )}
            /&gt;
            &lt;Route
              path=&quot;/gaming&quot;
              render={(props) =&gt; (
                &lt;GamingPage
                  {...props}
                  theme={propss.theme}
                  setTheme={propss.setTheme}
                /&gt;
              )}
            /&gt;
            &lt;Route
              path=&quot;/anime&quot;
              render={(props) =&gt; (
                &lt;AnimePage
                  {...props}
                  theme={propss.theme}
                  setTheme={propss.setTheme}
                /&gt;
              )}
            /&gt;
          &lt;/Switch&gt;
        &lt;/HashRouter&gt;
      &lt;/div&gt;
    );
  }
}

> 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.

示例:

import { HashRouter } from "react-router-dom";

...

<HashRouter>
  ...
  <Main />
  ...
</HashRouter>
import React from "react";
import { Route, Switch, useHistory } from "react-router-dom";
...

export default function Main(propss) {
  const history = useHistory();

  recognition.onresult = (event) => {
    const command = event.results[0][0].transcript;

    if (command.includes("navigate to") || command.includes("go to")) {
      if (command.includes("home") || command.includes("index")) {
        history.push("/home");
      } else if (command.includes("education") || command.includes("index")) {
        history.push("/education");
      } else if (command.includes("experience") || command.includes("index")) {
        history.push("/experience");
      } else if (command.includes("contact") || command.includes("index")) {
        history.push("/contact");
      }
    }
  };

  recognition.onend = () => {
    recognition.start();
  };

  if (settings.isSplash) {
    return (
      <div>
        <Switch>
          ...
        </Switch>
      </div>
    );
  } else {
    return (
      <div>
        <Switch>
          ...
        </Switch>
      </div>
    );
  }
}
英文:

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:

import { HashRouter } from &quot;react-router-dom&quot;;

...

&lt;HashRouter&gt;
  ...
  &lt;Main /&gt;
  ...
&lt;/HashRouter&gt;
import React from &quot;react&quot;;
import { Route, Switch, useHistory } from &quot;react-router-dom&quot;;
...

export default function Main(propss) {
  const history = useHistory();

  recognition.onresult = (event) =&gt; {
    const command = event.results[0][0].transcript;

    if (command.includes(&quot;navigate to&quot;) || command.includes(&quot;go to&quot;)) {
      if (command.includes(&quot;home&quot;) || command.includes(&quot;index&quot;)) {
        history.push(&quot;/home&quot;);
      } else if (command.includes(&quot;education&quot;) || command.includes(&quot;index&quot;)) {
        history.push(&quot;/education&quot;);
      } else if (command.includes(&quot;experience&quot;) || command.includes(&quot;index&quot;)) {
        history.push(&quot;/experience&quot;);
      } else if (command.includes(&quot;contact&quot;) || command.includes(&quot;index&quot;)) {
        history.push(&quot;/contact&quot;);
      }
    }
  };

  recognition.onend = () =&gt; {
    recognition.start();
  };

  if (settings.isSplash) {
    return (
      &lt;div&gt;
        &lt;Switch&gt;
          ...
        &lt;/Switch&gt;
      &lt;/div&gt;
    );
  } else {
    return (
      &lt;div&gt;
        &lt;Switch&gt;
          ...
        &lt;/Switch&gt;
      &lt;/div&gt;
    );
  }
}

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:

确定