javascript 重定向至“提交组件”诊断树

798qvoo8  于 2023-05-27  发布在  Java
关注(0)|答案(3)|浏览(330)

我的react应用程序在提交表单后没有重定向到 Jmeter 板页面。我尝试使用react-router-domRedirect函数,但仍然没有成功。然后我选择了BrowserHistory.push,但仍然没有成功。
对于后一个试验,URL更改为"/dashboard",但组件仍保留在表单页面上,直到我重新加载页面才移动到dashboard。
表格栏

<form>
  <Input
    displayValidationErrors={displayValidationErrors("fullname", validators)}
    name="fullname"
    type="text"
    label="Full Name"
    onChange={(e) => handleInputChange(e.target.name, e.target.value)}
  />
  <Input
    displayValidationErrors={displayValidationErrors("password", validators)}
    name="password"
    type="password"
    label="Password"
    onChange={(e) => handleInputChange(e.target.name, e.target.value)}
  />
  <Input
    displayValidationErrors={displayValidationErrors("password2", validators)}
    name="password2"
    type="password"
    label="Confirm Password"
    onChange={(e) => handleInputChange(e.target.name, e.target.value)}
  />
  <Input
    displayValidationErrors={displayValidationErrors("email", validators)}
    name="email"
    type="email"
    label="Email Address"
    onChange={(e) => handleInputChange(e.target.name, e.target.value)}
  />
  <Input
    displayValidationErrors={displayValidationErrors(
      "phone_number",
      validators
    )}
    name="phone_number"
    type="number"
    label="Phone Number"
    onChange={(e) => handleInputChange(e.target.name, e.target.value)}
  />
  <Input
    displayValidationErrors={displayValidationErrors("card_number", validators)}
    value={data.card_number}
    name="card_number"
    type="text"
    label="Card Number"
    onChange={(e) => handleInputChange(e.target.name, e.target.value)}
  />
  <Input
    displayValidationErrors={displayValidationErrors("date", validators)}
    value={data.date}
    name="date"
    type="text"
    label="Expiry Date"
    onChange={(e) => handleInputChange(e.target.name, e.target.value)}
  />
  <Input
    displayValidationErrors={displayValidationErrors("pin", validators)}
    name="pin"
    type="password"
    label="PIN"
    onChange={(e) => handleInputChange(e.target.name, e.target.value)}
  />
  <div className="col-lg-12 loginbttm">
    <div className=" login-btm login-button">
      <Button
        handleClick={onSubmit}
        type="submit"
        className="btn btn-primary"
        label="SUBMIT"
        disabled={!isFormValid(validators)}
      />
    </div>
  </div>
</form>;

onSubmit函数

const onSubmit = (e) => {
  e.preventDefault();
  browserHistory.push("/dashboard");
};

按钮组件

const Button = ({ type, className, handleClick, label, disabled }) => (
  <button
    type={type}
    className={className}
    onClick={handleClick}
    disabled={disabled}
  >
    {label}
  </button>
);

我的app.js

function App() {
  return (
    <Router>
      <div className="App">
        <Switch>
          <Route exact  path="/dashboard">
            <Dashboard />
          </Route>
          <Route exact path="/">
            <Form />
          </Route>
        </Switch>
      </div>
    </Router>
  );
}

export default App;
oxosxuxt

oxosxuxt1#

我没有看到createBrowserHistory或类似<Router history={history}>的东西,所以我猜你正在使用默认的浏览器历史记录。在这种情况下,您需要withRouter来使其工作:

import React from "react";
import { withRouter } from "react-router-dom";

function App() {
  const onSubmit = (e) => {
    e.preventDefault()
    this.props.history.push("/personalInfo");             
  }
  ...
}
export default withRouter(App);

更多关于withRouter解决方案here
更多关于createBrowserHistory解决方案here
对于带有react-router V5的功能组件,也可以使用hooks:

import { useHistory } from "react-router-dom";

function App() {
  let history = useHistory();
  const onSubmit = (e) => {
    e.preventDefault()
    history.push('/dashboard');        
  }

  return (
    <Router>
      <div className="App">
        ...
      </div>
    </Router>
  );
}

查看this以了解有关useHistory的更多信息

8hhllhi2

8hhllhi22#

试着在下面使用。希望能有帮助

this.props.history.push({
        pathname: '/dashboard'
})
uelo1irk

uelo1irk3#

另一种使用函数组件的方法-

import {withRouter} from "react-router-dom";

function SignIn({history}) {
    const submit = (event) => {
        event.preventDefault();
        history.push("/profile");
    }
    ....
}

export default withRouter(SignIn)

相关问题