Answers for "react on submit redirect"

6

redirect onclick react

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

function Home() {
  const history = useHistory();
  
  const handleRoute = () =>{ 
    history.push("/about");
  }
  
  return (                     
          <Button
            onClick={handleRoute}>
              About
          </Button>
  );
}
export default Home;
Posted by: Guest on December-16-2020
2

react redirect on click

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

function app() {
  let history = useHistory();

  const redirect = () => {
    history.push('/your-path')
  }

  return (
    <div>
      <button onClick={redirect}>Redirect</button>
    </div>
  )
}
Posted by: Guest on February-08-2021
0

how to redirect a form to another page when we submitted a form in react js

onSubmitHandler = (e) => {
    e.preventDefault();
    this.setState({
      showName: true
    });
    if(this.state.showName){
      // submitting the form conditionally with javascript
      document.getElementById("nameForm").submit();
    }
  }
  render() {
    return (
      <div>
        <form
          onSubmit={this.onSubmitHandler}
          id="nameForm"
          action="https://google.com"
          method="GET"
        >
          <label>Enter the Name</label>
          <input type="text"
            name="firstName" onChange={this.inputHandler} value=
            {this.state.firstName} />
          <button type="submit" onClick={this.onSubmitHandler}>Submit</button>
          {this.state.showName && <p>"FirstName: " {this.state.firstName}</p>}
        </form>
      </div>
    );
  }
Posted by: Guest on September-30-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language