Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

279
Visualizações
React event.preventDefault() from onChange submission does not prevent refreshing

I'm trying to make a range form where it sends the changed data to Flask backend on any change so it's seamless. Similar to how openCV trackbar works.

import { useState } from "react";

const SlideBarFg = (props) => {
  const [slideLowHue, setSlideLowHue] = useState('');

  const handleChangeLowHue = (event) => {
        setSlideLowHue(event.target.value);
        event.preventDefault();
        event.target.form.requestSubmit();
  };

  return(
  <form method="POST">
  <label htmlFor="lowHue">Lower Hue: {slideLowHue}</label>
            <input
                id = "lowHue"
                type = "range"
                min = {0}
                max = {179}
                step = {1.0}
                defaultValue = {0}
                onChange={handleChangeLowHue}
            />
  </form>
  );
};

export default SlideBarFg;

I have also tried adding a <form onSubmit = {submitHandler}> and did the event.preventDefault() there.

No matter what I tried it still refreshed and it seems not many have had this combination of having to submit onChange and not refresh, at least from what I found.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Thanks to @RandyCasburn

import { useState } from "react";

const SlideBarFg = (props) => {
  const [slideLowHue, setSlideLowHue] = useState(0);

const updateFg = () => {
        const specs = {lowHue: slideLowHue, upperHue: slideUpperHue,
                       lowSat: slideLowSat, upperSat: slideUpperSat,
                       lowBright: slideLowBright, upperBright: slideUpperBright};
        console.log(specs);
        
        props.onUpdateForeGround(specs);
    }
  const handleChangeLowHue = (event) => {
        setSlideLowHue(event.target.value);
        updateFg();
  };

  return(
  <form method="POST">
  <label htmlFor="lowHue">Lower Hue: {slideLowHue}</label>
            <input
                id = "lowHue"
                type = "range"
                min = {0}
                max = {179}
                step = {1.0}
                defaultValue = {0}
                onChange={handleChangeLowHue}
            />
  </form>
  );
};

export default SlideBarFg;

And in another file:

import SlideBarFg from "./SlideBarFg";
import regeneratorRuntime from "regenerator-runtime";

async function updateForeGroundHandler(specs){
  const response = await fetch('http://localhost:5000/d_object/capture/fg_specs/',{
    method: 'POST',
    body: JSON.stringify(specs),
    headers: {
      'Content-Type': 'application/json',
      'Accept': 'application/json',
    }
  })
}

And following is the python file:

from flask import request, jsonify

@d_object_blueprint.route('/capture/fg_specs/', methods = ['GET', 'POST'])
def fg_specs_request():
    data = request.data
    print(data)
    return(data)

Also at least in my cases I had to do the following:

from flask_cors import CORS

app = Flask(__name__)
CORS(app)
about 4 years ago · Juan Pablo Isaza Relatório

0

You'll need to write your own function for sending the data to your backend, and call that instead of requestSubmit().

preventDefault() is preventing requestSubmit(), which you're then calling manually. That's whats causing the behavior you're trying to prevent :)

Your function will need to have some kind of AJAX call in it that sends the data to the correct endpoint in your flask API, and then handles the response however you need.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda