简体   繁体   中英

React componentDidMount "Parsing error: Missing semicolon"

"Parsing error: Missing semicolon" .

SyntaxError: client\src\App.js: Missing semicolon (15:21)

This error is showing on componentDidMount() line.

and the full code is given below.

import React, { Component } from "react";
import AppNavBar from "./components/AppNavbar";
import ShoppingList from "./components/ShoppingList";
import ItemModal from "./components/ItemModal";
import { Container } from "reactstrap";

import { Provider } from "react-redux";
import store from "./store";
import { loadUser } from "./actions/authActions";

import "bootstrap/dist/css/bootstrap.min.css";
import "./App.css";

function App() {
  componentDidMount() {
    store.dispatch(loadUser());
  }
  return (
    <Provider store={store}>
      <div className="App">
        <AppNavBar />
        <Container>
          <ItemModal />
          <ShoppingList />
        </Container>
      </div>
    </Provider>
  );
}

export default App;

错误截图

Import useEffect:

import { useEffect } from "react";

... and replace:

componentDidMount() {
  store.dispatch(loadUser());
}

with:

useEffect(() => {
  store.dispatch(loadUser());
}, []);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM