Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

225
Views
prevent refresh of website on onClick funtion in react js

i am trying to submit a form but whenever i submit the form the entire website gets refreshed i dont want that to happen i want to submit the form without refreshing

import { useEffect, useState } from "react";
import axios from "axios";
export default function Add() {
  const [close, setClose] = useState(false);
  const [allValues, setallValues] = useState("");

  function closeDialog() {
    setClose(true);
  }
  function changeHandler(e) {
    setallValues(e.target.value);
  }
  const submitValue = (e) => {
    e.preventDefault();
    const symptom = allValues;

    const { name } = e.target.dataset;
    const Data = {
      add: allValues,
    
    };
    console.log(allValues);
    //setAllValues({ ...all
    const headers = {
      Authorization: `token ` + localStorage.getItem("token"),
    };
    axios
      .post("apiadd/", Data, {
        headers: headers,
      })
      .then(() => {
        alert(" submitted");
      })
      .catch((error) => {
        alert("Cannot add  again");
      });
  };
  return (
    <>
      {close ? null : (
        <div style={styles.cover}>
          <h3>Add New </h3>
          <hr />

          <form style={styles.form} onSubmit={submitValue}>
            <span>add</span>
            <input
              style={styles.input}
              type="text"
              onChange={changeHandler}
              defaultValue={allValues}
              //value=""
            />
            <input style={styles.sbtn} type="submit" value="ADD" />
          </form>
          <button style={styles.dbtn} onClick={closeDialog}>
            Done
          </button>
        </div>
      )}
    </>
  );
}

i am trying to submit a form but whenever i submit the form the entire website gets refreshed i dont want that to happen i want to submit the form without refreshing

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!