【问题标题】:Uncaught TypeError: Cannot read properties of undefined (reading 'state')未捕获的类型错误:无法读取未定义的属性(读取“状态”)
【发布时间】:2022-01-01 00:49:42
【问题描述】:

我正在尝试创建一个新航班并在控制台中显示其属性值,并在提交后立即返回主页。但是,当我运行服务器时,会出现此错误(我使用的是 react-router-dom v6):

TypeError:无法读取未定义的属性(读取“状态”) 提交

CreateFlight 组件的代码如下:

import React, { Component } from 'react';
import { Container } from 'react-bootstrap';
import DatePicker from 'react-datepicker';
import "react-datepicker/dist/react-datepicker.css";


export class CreateFlight extends Component {
    constructor(props) {
        super(props);

        this.onChangeFrom = this.onChangeFrom.bind(this);
        this.onChangeTo = this.onChangeTo.bind(this);
        this.onChangeCabin = this.onChangeCabin.bind(this);
        this.onChangeSeatNumber = this.onChangeSeatNumber.bind(this);
        this.onChangeFlightDate = this.onChangeFlightDate.bind(this);
        this.onChangeDepartureTime = this.onChangeDepartureTime.bind(this);
        this.onChangeArrivalTime = this.onChangeArrivalTime.bind(this);
        this.onChangeTerminal = this.onChangeTerminal.bind(this);

        this.state = {
            from: '',
            to: '',
            cabin: '',
            seatNumber: 0,
            flightDate: new Date(),
            departureTime: '',
            arrivalTime: '',
            terminal: 0,

        }
    }


    onChangeFrom(e) {
        this.setState({
            from: e.target.value
        });
    }

    onChangeTo(e) {
        this.setState({
            to: e.target.value
        });
    }

    onChangeCabin(e) {
        this.setState({
            cabin: e.target.value
        });
    }

    onChangeSeatNumber(e) {
        this.setState({
            seatNumber: e.target.value
        });
    }

    onChangeFlightDate(date) {
        this.setState({
            flightDate: date
        });
    }

    onChangeDepartureTime(e) {
        this.setState({
            departureTime: e.target.value
        });
    }

    onChangeArrivalTime(e) {
        this.setState({
            arrivalTime: e.target.value
        });
    }

    onChangeTerminal(e) {
        this.setState({
            terminal: e.target.value
        });
    }

    onSubmit(e) {
        e.preventDefault();

        const flight = {
            from: this.state.from,
            to: this.state.to,
            cabin: this.state.cabin,
            seatNumber: this.state.seatNumber,
            flightDate: this.state.flightDate,
            departureTime: this.state.departureTime,
            arrivalTime: this.state.arrivalTime,
            terminal: this.state.terminal

        }

        console.log(flight)

        window.location = '/home';
    }
    render() {
        return (
            <Container>
                <div><br/>
                    <h3>Create New Flight </h3>
                    <form onSubmit={this.onSubmit}>
                        <div className="form-group">
                            <label>From: </label>
                            <input type="text"
                                required
                                className="form-control"
                                placeholder="Enter departure city"
                                value={this.state.from}
                                onChange={this.onChangeFrom}
                            />
                        </div>
                        <div className="form-group">
                            <label>To: </label>
                            <input type="text"
                                required
                                className="form-control"
                                placeholder="Enter destination city"
                                value={this.state.to}
                                onChange={this.onChangeTo}
                            />
                        </div>
                        <div className="form-group">
                            <label>Cabin: </label>
                            <input type="text"
                                required
                                className="form-control"
                                placeholder="Choose cabin class"
                                value={this.state.cabin}
                                onChange={this.onChangeCabin}
                            />
                        </div>
                        <div className="form-group">
                            <label>Seat Number: </label>
                            <input type="text"
                                required
                                className="form-control"
                                placeholder="Choose seat number"
                                value={this.state.seatNumber}
                                onChange={this.onChangeSeatNumber}
                            />
                        </div>
                        <div className="form-group">
                            <label>Flight Date: </label>
                            <div>
                                <DatePicker
                                    selected={this.state.flightDate}
                                    onChange={this.onChangeFlightDate}
                                />
                            </div>
                        </div>
                        <div className="form-group">
                            <label>Departure Time: </label>
                            <input type="text"
                                required
                                className="form-control"
                                placeholder="Enter departure time"
                                value={this.state.departureTime}
                                onChange={this.onChangeDepartureTime}
                            />
                        </div>
                        <div className="form-group">
                            <label>Arrival Time: </label>
                            <input type="text"
                                required
                                className="form-control"
                                placeholder="Enter arrival time"
                                value={this.state.arrivalTime}
                                onChange={this.onChangeArrivalTime}
                            />
                        </div>
                        <div className="form-group">
                            <label>Terminal: </label>
                            <input type="text"
                                required
                                className="form-control"
                                placeholder="Choose terminal number"
                                value={this.state.terminal}
                                onChange={this.onChangeTerminal}
                            />
                        </div><br/>

                        <div className="form-group">
                            <input type="submit" value="Create Flight" className="btn btn-primary" />
                        </div>
                    </form>
                </div>
            </Container>
        )
    }
}

这是渲染 CreateFlight 组件的 App 组件的代码:



import './App.css';
import {Home} from './components/Home';
import {AvailableFlights} from './components/AvailableFlights';
import {FlightsSchedule} from './components/FlightsSchedule';
import {CreateFlight} from './components/CreateFlight';
import {Login} from './components/Login';
import {FindFlight} from './components/FindFlight';
import {Footer} from './components/Footer';
import {BrowserRouter, Route, Routes} from 'react-router-dom';
import { NavDropdown, Container, Navbar, Nav, Form, Button, FormControl} from "react-bootstrap";
import 'bootstrap/dist/css/bootstrap.min.css';





function App() {
  return (
    <BrowserRouter>
    <Navbar bg="dark" expand="lg" variant="dark" sticky="top" >
    <Container fluid>
      <Navbar.Brand id="nav_title" href="/home">Airline Reservation</Navbar.Brand>
      <Navbar.Toggle aria-controls="navbarScroll" />
      <Navbar.Collapse id="navbarScroll">
        <Nav
          className="me-auto my-2 my-lg-0"
          style={{ maxHeight: '100px' }}
          navbarScroll
        >
          <Nav.Link href="/home">Home</Nav.Link>
          <Nav.Link href="/login">Login</Nav.Link>
          <NavDropdown title="Flights" id="navbarScrollingDropdown">
            <NavDropdown.Item href="/availableFlights">Available Flights</NavDropdown.Item>
            <NavDropdown.Item href="/flightsSchedule">Flights Schedule</NavDropdown.Item>
            <NavDropdown.Item href="/createFlight">Create Flight</NavDropdown.Item>
            {/* <NavDropdown.Divider /> */}
            {/* <NavDropdown.Item href="#action5">
              Something else here
            </NavDropdown.Item> */}
          </NavDropdown>
        </Nav>
        <Form className="d-flex">
          <FormControl
            type="search"
            placeholder="Search"
            className="me-2"
            aria-label="Search"
          />
          <Button variant="outline-success">Search</Button>
        </Form>
      </Navbar.Collapse>
    </Container>
  </Navbar>
       <Routes>
         <Route path='/home' element={<Home />}/>
         <Route path='/login' element={<Login />}/>
         <Route path='/availableFlights' element={<AvailableFlights />}/>
         <Route path="/flightsSchedule" element={<FlightsSchedule />}/>
         <Route path='/createFlight' element={<CreateFlight />}/>
         <Route path='/findFlight' element={<FindFlight />}/>   
       </Routes>
       <Footer />
  </BrowserRouter>
  );
}

export default App;

【问题讨论】:

    标签: reactjs react-router-dom


    【解决方案1】:

    您应该在构造函数中为onSubmit 方法绑定this

    
    this.onSubmit = this.onSubmit.bind(this);
    
    

    建议:您可以使用带有钩子的功能组件来提高您的速度和维护代码的功能。

    【讨论】:

    • 谢谢! @SaeedHemmati。是的,我其实想用 Hooks 代替,但我不知道它的语法在最新更新(dom v6)中是怎么写的,你知道它是怎么做的吗?
    • 欢迎您。将类组件重构为功能组件取决于应用程序的规模,并且在重构后需要进行许多测试。因此,您可以将钩子用于新项目而不是现有项目。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-22
    • 2023-03-21
    • 2021-12-25
    • 2021-11-24
    • 2021-10-31
    • 2021-11-07
    • 2022-01-17
    相关资源
    最近更新 更多