简体   繁体   English

react Js - × TypeError:无法读取未定义的属性(读取'params'

[英]react Js - × TypeError: Cannot read properties of undefined (reading 'params'

Here im trying to edit a post on my web app.我在这里尝试编辑我的网络应用程序上的帖子。 But when I it says × TypeError: Cannot read properties of undefined (reading 'params' can someone point out the error. im doing this wacthing a tutorial. It works fine. but in my code it doesnt work. and I tried importing useParams and using it. but it says useParams cannot use in class components. can someone point out the error or give me a fix please?但是当我说 × TypeError:无法读取未定义的属性(读取“参数”可以有人指出错误。我正在做这个教程。它工作正常。但在我的代码中它不起作用。我尝试导入 useParams 和使用它。但它说 useParams 不能在类组件中使用。有人可以指出错误或给我一个修复吗?

import React, {Component} from 'react';
import axios from 'axios';
import {Dropdown} from "react-bootstrap";
import {useParams} from "react-router-dom";


class Feed2 extends Component {
    constructor(props){
        super(props);
        this.state={
            vehicle:"",
            plateNo:"",
            owner:"",
            manufacturer:"",
            manufacturedYear:"",
            color:""
        }
        
    }


    takInput = (e) => {
        const {name,value}= e.target;
        this.setState({
            ...this.state,
            [name]:value
        })
    }

    register = (e) =>{
        e.preventDefault();
        const id = this.props.match.params.id;
        const {plateNo,owner,manufacturer,manufacturedYear,color,vehicle} = this.state;
        const data ={
            plateNo:plateNo,
            owner:owner,
            manufacturer:manufacturer,
            manufacturedYear:manufacturedYear,
            color:color,
            vehicle:vehicle
        }

        console.log(data);
        axios.put(`http://localhost:8080/registrations/update/${id}`,data).then((res)=>{
            if(res.data.success){
                alert("Post Updated");
                this.setState({
                    vehicle:"",
                    plateNo:"",
                    owner:"",
                    manufacturer:"",
                    manufacturedYear:"",
                    color:""
                })
            }
        })
    }
    componentDidMount(){
        const id = this.props.match.params.id;
        axios.get(`/registrations/${id}`).then((res) =>{
            if(res.data.success){
                this.setState({
                   plateNo:res.data.registrations.plateNo,
                    owner:res.data.registrations.owner,
                    manufacturer:res.data.registrations.manufacturer,
                    manufacturedYear:res.data.registrations.manufacturedYear,
                    color:res.data.registration.color,
                    vehicle:res.data.registrations.vehicle
                })
                console.log(this.state.registrations)
            }
        })
    }


    render() {
        return (
            <div className="container">
                <div className="register-box">
                    <div className="Top">

                        <div className="box">
                            <input placeholder="Enter the licence plate number "
                                   className="input2" name="plateNo" value={this.state.plateNo} onChange={this.takInput} />
                        </div>

                    </div>
                    <div className="box">
                        <input placeholder="Enter the owners name "
                               className="input2" name="owner" value={this.state.owner} onChange={this.takInput} />
                    </div>
                    <div className="box">
                        <input placeholder=" Manufactured Year "
                               className="input2" name="manufacturedYear" value={this.state.manufacturedYear} onChange={this.takInput} />
                    </div>
                    <div className="box">
                        <input placeholder="Manufacturer"
                               className="input2" name="manufacturer" value={this.state.manufacturer} onChange={this.takInput} />
                    </div>
                    <div className="box">
                        <input placeholder="Color of the vehicle "
                               className="input2" name="color" value={this.state.color} onChange={this.takInput} />
                    </div>
                    <div className="box">
                        <input placeholder="Vehicle name "
                               className="input2" name="vehicle" value={this.state.vehicle} onChange={this.takInput} />
                    </div>

                    <Dropdown className="drop">
                        <Dropdown.Toggle variant="success" id="dropdown-basic">
                            Select Vehicle Type
                        </Dropdown.Toggle>

                        <Dropdown.Menu>
                            <Dropdown.Item href="#/action-1">Car</Dropdown.Item>
                            <Dropdown.Item href="#/action-2">Van</Dropdown.Item>
                            <Dropdown.Item href="#/action-3">Bus</Dropdown.Item>
                            <Dropdown.Item href="#/action-3">Lorry</Dropdown.Item>
                            <Dropdown.Item href="#/action-3">Three-weeler</Dropdown.Item>
                            <Dropdown.Item href="#/action-3">Bike</Dropdown.Item>
                            <Dropdown.Item href="#/action-3">Other</Dropdown.Item>

                        </Dropdown.Menu>
                    </Dropdown>



                    <div className="regbuttonContainer">
                        <button className="Button" onClick={this.register}>Register Vehicle</button>
                    </div>
                </div>

            </div>
        );
    }
}


export default Feed2;

I guess you need to wrap this component with withRouter HOC, So it be like export default withRouter(Feed2);我猜你需要用 withRouter HOC 包装这个组件,所以它就像 export default withRouter(Feed2);

and import withRouter from 'react-router-dom';并从 'react-router-dom' 导入 withRouter;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未捕获的类型错误:无法读取未定义的属性(读取“参数”) - Uncaught TypeError: Cannot read properties of undefined (reading 'params') ProductScreen.js:12 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'params') - ProductScreen.js:12 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'params') TypeError:无法在 react.js 中的 isURLSameOrigin (isURLSameOrigin.js:57:1) 处读取未定义的属性(读取“协议”) - TypeError: Cannot read properties of undefined (reading 'protocol') at isURLSameOrigin (isURLSameOrigin.js:57:1) in react.js React js TypeError:无法读取未定义的属性“参数” - React js TypeError: Cannot read property 'params' of undefined 我该如何解决这个问题? 类型错误:无法读取 react.js 中未定义(读取“地图”)的属性 - How can i solve this issue ? TypeError: Cannot read properties of undefined (reading 'map') in react.js 从反应和节点js中的API检索数据时出现问题:TypeError:无法读取未定义的属性(读取&#39;0&#39;) - Issue when retrieving data from an API in react and node js : TypeError: Cannot read properties of undefined (reading '0') TypeError:无法读取未定义的属性(读取“原型”)React APP - TypeError: Cannot read properties of undefined (reading 'prototype') React APP 类型错误:无法使用 Axios 在 React 中读取未定义的属性(读取“协议”) - TypeError: Cannot read properties of undefined (reading 'protocol') in React using Axios TypeError:无法在 React-Views 中读取未定义的属性(读取“地图”) - TypeError: Cannot read properties of undefined (reading 'map') in React-Views Discord.js:TypeError:无法读取未定义的属性(读取“添加”) - Discord.js: TypeError: Cannot read properties of undefined (reading 'add')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM