Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

223
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda