简体   繁体   English

Props Match Params Id 无法读取且 Props.id 未定义

[英]Props Match Params Id can't be read and Props.id is Undefined

If I am using const currentUserId = props.id then the error will be props.match is undefined , and the variable "userId" even is undefined when i am trying to console log.如果我使用const currentUserId = props.id那么错误将是props.match is undefined ,当我尝试控制台日志时,变量“userId”甚至是未定义的。 在此处输入图像描述

Then, I using props.match.params.id the code can't even being read, it's only blank然后,我使用props.match.params.id代码甚至无法读取,它只是空白在此处输入图像描述

Im also trying to modify the code become const currentUserId = props.computedMatch.params.id;我也在尝试将代码修改为const currentUserId = props.computedMatch.params.id; but nothing works但没有任何效果在此处输入图像描述

My code:我的代码:

import React, {useContext, useState, useEffect} from 'react';
import { GlobalContext } from '../context/GlobalState';
import { Link, useNavigate } from 'react-router-dom';
import { v4 as uuid } from 'uuid';
import {
  Form,
  FormGroup,
  Label,
  Input,
  Button
} from 'reactstrap';

export const EditUser = (props) => {
  const [selectedUser, setSelectedUser] = useState({
    id: '',
    name: ''
  });
  const { users, editUser } = useContext(GlobalContext);
  const history = useNavigate();
  const currentUserId = props.match.params.id;

  useEffect(() => {
    const userId = currentUserId;
    console.log(typeof userId);
    const selectedUser = users.find(user => user.id === Number(userId))
    setSelectedUser(selectedUser)
    console.log(selectedUser);
  }, [currentUserId, users])

  const onSubmit = () => {
  
    history('/');
  }

  const onChange = (e) => {

  }
  return (
    <Form onSubmit={onSubmit}>
      <FormGroup>
        <Label>Nama</Label>
        <Input type='text' onChange={onChange} placeholder='Ganti Nama Disini!'></Input>
      </FormGroup>
      <Button type='submit'>Edit Data</Button>
      <Link to="/" className="btn btn-danger ml-2" style={{ marginLeft: "10px"}}>Cancel</Link>
    </Form>
  )
}

use withRouter与路由器一起使用

 import React, {useContext, useState, useEffect} from 'react'; import { GlobalContext } from '../context/GlobalState'; import { Link, useNavigate } from 'react-router-dom'; import { withRouter } from 'react-router'; // here added import { v4 as uuid } from 'uuid'; import { Form, FormGroup, Label, Input, Button } from 'reactstrap'; const EditUser = (props) => { // here changed const [selectedUser, setSelectedUser] = useState({ id: '', name: '' }); const { users, editUser } = useContext(GlobalContext); const history = useNavigate(); const currentUserId = this.props.match.params.id; useEffect(() => { const userId = currentUserId; console.log(typeof userId); const selectedUser = users.find(user => user.id === Number(userId)) setSelectedUser(selectedUser) console.log(selectedUser); }, [currentUserId, users]) const onSubmit = () => { history('/'); } const onChange = (e) => { } return ( <Form onSubmit={onSubmit}> <FormGroup> <Label>Nama</Label> <Input type='text' onChange={onChange} placeholder='Ganti Nama Disini!'></Input> </FormGroup> <Button type='submit'>Edit Data</Button> <Link to="/" className="btn btn-danger ml-2" style={{ marginLeft: "10px"}}>Cancel</Link> </Form> ) } export default withRouter(EditUser); // here added

i hope this will fix我希望这会解决

import { useParams } from 'react-router-dom';
const { id: currentUserId } = useParams();

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

相关问题 你能告诉我为什么我的 props.id 是未定义的吗 - Can you tell me why my my props.id is coming out to be undefined 为什么 Vue 3 的 `setup(props) { props.id }` 返回 undefined - Why does Vue 3's `setup(props) { props.id }` return undefined 道具验证中缺少“match.params.id” - 'match.params.id' is missing in props validation 我无法在 React.js 中访问“this.props.match.params.id” - I can't access “this.props.match.params.id” in React.js 为什么firestoreConnect不读取&#39;props.match.params.id&#39; - &#39;where&#39;使用react.js和redux的集合查询? - Why is the 'props.match.params.id' not read by firestoreConnect - 'where' collection query using react.js & redux? 嵌套路线和Switch-如何传递props.match.params.id? - Nested routes and Switch - how to pass props.match.params.id? React:我可以将div ID与Props匹配吗 - React: Can I match the div id with Props MERN:TypeError:未定义不是对象(正在评估“ this.props.params.id”) - MERN: TypeError: undefined is not an object (evaluating 'this.props.params.id') 在 reactjs v6 或 this.props.match.params.id 中调用请求时获取 ID null 无效 - Getting id null while calling a request in reactjs v6 or this.props.match.params.id not working React Meteor教程无法删除this.props.post._id - React Meteor Tutorial can't remove this.props.post._id
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM