skip to Main Content

fetch is making multiple unwanted requests, I tried the below but it still makes 6 to 10 requests

const [allowParse, setAllowParse] = useState<boolean>(true);
socket.on("upload_finished", function (response) {
        if (allowParse === true) {
            console.log("parse start");
            fetch("start-parse", {method: "POST"});
            setAllowParse(false)
        }
    });

I also looked into this post but I can’t use useEffect inside a conditional statement

How do I fix this?

If you are wondering how I receive upload_finished I am using flask socket-io

@app.route("/upload", methods=["POST", "GET"])
def upload_file():
    # Code handling file upload
    socketio.emit("upload_finished")

2

Answers


  1. Chosen as BEST ANSWER

    Well I found a workaround but it only works if you are using websockets

    Instead of using socket.on("event", () => {"do something"} we can use socket.once


  2. You should wrap the whole socket.on in a useEffect. I can’t see why you wouldn’t be able to do that, unless you share the entire component file.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search