GithubHelp home page GithubHelp logo

Bug: about react HOT 14 OPEN

mayank1513 avatar mayank1513 commented on May 3, 2024 2
Bug:

from react.

Comments (14)

den319 avatar den319 commented on May 3, 2024 2

Hi, @mayank1513 can you try the code I have mentioned below , I think it will solve your problem and let me know if there is anything wrong.

`export default function App() {
const [clicked, setClicked] = useState(false);

return (
<form
onSubmit={(e) => {
e.preventDefault();
console.log("form submitted!");
}}
>
{!clicked ? (
<button type="submit" onClick={() => setClicked(true)}>
"button"

) : (
<button type="button" onClick={() => setClicked(false)}>
"submit"

)}

);
}`

from react.

mayank1513 avatar mayank1513 commented on May 3, 2024 1

Hi, why are you tagging type="submit" as "button" and vice-versa? These values are used to demonstrate the current working and understand why it is working this way.

from react.

mayank1513 avatar mayank1513 commented on May 3, 2024 1

Action corresponding to the button which I click should be triggered. Not the other button.

from react.

matheusschreiber avatar matheusschreiber commented on May 3, 2024 1

This behavior that you are seeing is because the form's onSubmit event is triggered regardless of the button type always.... Maybe you should move your state modifiers outside the buttons

Try this

import { useState } from "react";

export default function App() {
  const [clicked, setClicked] = useState(false);

  const handleFormSubmit = (e) => {
    e.preventDefault();
    if (clicked) {
      setClicked(false)
      console.log('form submitted!');
      // Perform other necessary actions
    } else {
      setClicked(true)
    }
  };

  return (
    <form>
      {!clicked ? (
        <button type="button" onClick={(e)=>handleFormSubmit(e)}>
          Type "button"
        </button>
      ) : (
        <button type="submit" onClick={(e)=>handleFormSubmit(e)}>
          Type "submit"
        </button>
      )}
    </form>
  );
}

You can also use e.target.form.submit inside the handler to in fact submit the form.

from react.

mayank1513 avatar mayank1513 commented on May 3, 2024 1

Please check the code sandbox carefully. Event is triggered only for one type of button, not both. You can even verify this by removing the condition and rendering both buttons together.

from react.

vitaliyirtlach avatar vitaliyirtlach commented on May 3, 2024 1

I changed code and add a preventDefault to button

import { useState } from "react";

export default function App() {
  const [clicked, setClicked] = useState(false);

  return (
    <form
      onSubmit={(e) => {
        e.preventDefault();
        setClicked(true);
        console.log("form submitted!");
      }}
    >
      {!clicked ? (
        <button type="submit">Type "submit"</button>
      ) : (
        <button
          type="button"
          onClick={(e) => {
            e.preventDefault();
            setClicked(false);
          }}
        >
          Type "button"
        </button>
      )}
    </form>
  );
}

from react.

arslanarjumand012 avatar arslanarjumand012 commented on May 3, 2024 1

Greetings @mayank1513 Here is the modified code.

import { useState } from "react";

export default function App() {
const [clicked, setClicked] = useState(false);

return (
<form
onSubmit={(e) => {
e.preventDefault();
console.log("form submitted!");
}}
>
{!clicked ? (
<button
type="button"
onClick={() => {
const timer = setTimeout(() => {
setClicked(true);
clearTimeout(timer);
}, 0);
}}
>
Type "button"

) : (
<button
type="submit"
onClick={() => {
const timer = setTimeout(() => {
setClicked(false);
clearTimeout(timer);
}, 0);
}}
>
Type "submit"

)}

);
}

The issue was that React updates the virtual dom faster than the real dom. So we must add the async behaviour to the onClick event where the state is being changed.

from react.

den319 avatar den319 commented on May 3, 2024

from react.

vitaliyirtlach avatar vitaliyirtlach commented on May 3, 2024

To resolve it, I assigned each button unique key prop. Hope this helped solve the problem

from react.

mayank1513 avatar mayank1513 commented on May 3, 2024

To resolve it, I assigned each button unique key prop. Hope this helped solve the problem

After assigning a unique key, the form-submit event is not fired at all.

from react.

den319 avatar den319 commented on May 3, 2024

so can you explain why this unususal behaviour was happening? I mean when we are clicking "type= button" button the onSubmit event was triggering.

from react.

mayank1513 avatar mayank1513 commented on May 3, 2024

I changed code and add a preventDefault to button

import { useState } from "react";

export default function App() {
  const [clicked, setClicked] = useState(false);

  return (
    <form
      onSubmit={(e) => {
        e.preventDefault();
        setClicked(true);
        console.log("form submitted!");
      }}
    >
      {!clicked ? (
        <button type="submit">Type "submit"</button>
      ) : (
        <button
          type="button"
          onClick={(e) => {
            e.preventDefault();
            setClicked(false);
          }}
        >
          Type "button"
        </button>
      )}
    </form>
  );
}

This does not address the issue.

from react.

meetvanapariya avatar meetvanapariya commented on May 3, 2024
import { useState } from "react";

export default function App() {
  const [clicked, setClicked] = useState(true);

  return (
    <form
      onSubmit={(e) => {
        e.preventDefault();
        console.log("form submitted!");
      }}
    >
      {clicked && (
        <button type="submit" onClick={() => setClicked(false)}>
          Type "submit"
        </button>
      )}
      <button type="submit" onClick={() => setClicked(true)}>
        Type "submit 2"
      </button>
    </form>
  );
}

i believe when state change from false to true it will immediately remove Type "submit" button and it will also clean pending effects like form submit check above code

  • conditional rendering and without conditional rendering

from react.

mayank1513 avatar mayank1513 commented on May 3, 2024

Greetings @mayank1513 Here is the modified code.

import { useState } from "react";

export default function App() {
const [clicked, setClicked] = useState(false);

return (
<form
onSubmit={(e) => {
e.preventDefault();
console.log("form submitted!");
}}
>
{!clicked ? (
<button
type="button"
onClick={() => {
const timer = setTimeout(() => {
setClicked(true);
clearTimeout(timer);
}, 0);
}}
>
Type "button"

) : (
<button
type="submit"
onClick={() => {
const timer = setTimeout(() => {
setClicked(false);
clearTimeout(timer);
}, 0);
}}
>
Type "submit"

)}

);
}

The issue was that React updates the virtual dom faster than the real dom. So we must add the async behaviour to the onClick event where the state is being changed.

This is not the expected behavior. Moreover, when you add unique keys to buttons, none of them submit. We are not looking for workarounds, but trying to improve React library itself to handle this gracefully.

from react.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.