简体   繁体   English

this.props.history.push('/') 在 CLASS 组件中不起作用

[英]this.props.history.push('/') IS NOT WORKING in CLASS Component

Please help me to resolve the issue of this.props.history.push('/') IS NOT WORKING in CLASS Component.请帮助我解决 this.props.history.push('/') IS NOT WORKING in CLASS Component 的问题。 As we do not have any scope of using history anymore.因为我们没有任何 scope 的使用历史了。 Unable to implement navigate.无法实施导航。 Please do help.请帮忙。 Same issue with props.location.state.contact.与 props.location.state.contact 相同的问题。

**const { name, email } = props.location.state.contact;**
import React, { Component } from "react";
import "../assets/css/AddContact.css";
import { Navigate } from "react-router-dom";
import { v4 as uuid } from "uuid";

class AddContact extends Component {
  state = {
    id: uuid(),
    name: "",
    email: "",
  };

 
  add = (e) => {
    e.preventDefault();
    if (this.state.name === "" || this.state.email === "") {
      alert("All fields are required!");
      return;
    }
    this.props.addContactHandler(this.state);
    this.setState({ name: "", email: "" });
    this.props.history.push("/");
  };

  render() {
    return (
      <div className="contactForm">
        <h2 className="contactForm__title">Add Contact</h2>
        <div className="contactForm__form">
          <form action="/" method="post" onSubmit={this.add}>
            <div className="contactForm__nameField">
              <label htmlFor="name">Name</label>
              <br />
              <input
                type="text"
                placeholder="Enter your name"
                name="name"
                id="name"
                value={this.state.name}
                onChange={(e) => this.setState({ name: e.target.value })}
              />
            </div>
            <div className="contactForm__emailField">
              <label htmlFor="email">Email</label>
              <br />
              <input
                type="email"
                placeholder="Enter your email"
                name="email"
                id="email"
                value={this.state.email}
                onChange={(e) => this.setState({ email: e.target.value })}
              />
            </div>
            <button className="contactForm__button">Add</button>
          </form>
        </div>
      </div>
    );
  }
}

export default AddContact;

I tried out from all of my references.我尝试了所有参考资料。

You need to use the withRouter Higher Order Component provided with the React Router library in order to get access to those props ( history and location automatically).您需要使用 React Router 库随附的withRouter高阶组件才能访问这些道具(自动historylocation )。

So just import that, and change your export to所以只需导入它,然后将导出更改为

export default withRouter(AddContact);

[Note that this assumes you are using React Router v5 or before - there is no withRouter in v6 which is the latest version. [请注意,这假设您使用的是 React Router v5 或之前的版本——最新版本的 v6 中没有withRouter But your use of a class component implies that you are using an earlier version - v6 only works well if you use function components, as withRouter has been replaced with Hooks.]但是您使用 class 组件意味着您使用的是早期版本 - v6 只有在您使用 function 组件时才能正常工作,因为withRouter已被 Hooks 取代。]

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

相关问题 ReactJs this.props.history.push()不起作用 - ReactJs this.props.history.push() is not working this.props.history.push() 在 ReactJS 中不起作用 - this.props.history.push() not working in ReactJS this.props.history.push刚刚刷新后正在运行 - this.props.history.push is working just after refresh React-router v4 this.props.history.push(...) 不工作 - React-router v4 this.props.history.push(...) not working this.props.history.push 在使用 HOC 记录器时不起作用 - this.props.history.push is not working while using HOC for logger 当我们使用 this.props.history.push(&quot;/next Component&quot;) 移动到下一个组件时,我们可以传递一个值吗? - can we pass a value when we move to next component using this.props.history.push("/next Component")? 当我在同一路由中传递回调函数时 this.props.history.push() 不起作用 - this.props.history.push() is not working when i pass the callback function in same route 类型错误:无法使用 this.props.history.push 读取未定义的属性“推送” - TypeError: Cannot read property 'push' of undefined with this.props.history.push this.props.history.push 适用于某些组件而不适用于其他组件 - this.props.history.push works in some components and not others TypeError: undefined is not an object (评估 &#39;this.props.history.push&#39;) - TypeError: undefined is not an object (evaluating 'this.props.history.push')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM