Tuesday, 22 January 2019

How to prevent full post back/ ( round-trip of form) in ReactJS?

Look into the highlighted code. It will prevent the full postback or a round trip of the requested URL.

import React, { Component } from 'react';

class LoginForm extends Component {
    state = {  }
    handleSubmit = e => {
        // Call the server
        e.preventDefault();
        console.log("Submitted");
      };
    render() {
        return (
            <div>
                <div class="form-row">
                <form onSubmit={this.handleSubmit}>
                    <div className="form-group col-md-12">
                        <label htmlFor="inputEmail1">Email address</label>
                        <input type="email" className="form-control" id="inputEmail1" aria-describedby="emailHelp" placeholder="Enter email" />
                        <small id="emailHelp" className="form-text text-muted">We'll never share your email with anyone else.</small>
                    </div>
                    <div className="form-group col-md-12">
                        <label htmlForfor="InputPassword1">Password</label>
                        <input type="password" className="form-control" id="InputPassword1" placeholder="Password" />
                    </div>
                    <div className="form-check">
                        <input type="checkbox" className="form-check-input" id="Check1" />
                        <label className="form-check-label" for="Check1">Check me out</label>
                    </div>
                    <button type="submit" className="btn btn-primary m-2">Submit</button>
                </form>
                </div>
            </div>
         );
    }
}

export default LoginForm;



No comments:

Post a Comment