GithubHelp home page GithubHelp logo

Comments (11)

mattcarrollcode avatar mattcarrollcode commented on December 10, 2024

Standard HTML forms provide the name and value of the button that submitted the form even if the button is outside the form tag: https://codesandbox.io/p/sandbox/practical-smoke-h6s2wp?file=%2Fapp.js%3A11%2C24

from react.

SiddiqAM1 avatar SiddiqAM1 commented on December 10, 2024

import React, { useRef } from "react";

const Component = () => {
const submitButtonRef = useRef(null);

function action(formData) {
console.log(formData.get("n1"));
}

function handleSubmit(event) {
event.preventDefault();
submitButtonRef.current.click();
}

return (
<>

Submit





</>
);
};

export default Component;

from react.

SiddiqAM1 avatar SiddiqAM1 commented on December 10, 2024

Use a hidden submit button inside of the form tag and programmatically click it when the external button is clicked.

from react.

SiddiqAM1 avatar SiddiqAM1 commented on December 10, 2024

you can also use React Hook Form Library

from react.

mattcarrollcode avatar mattcarrollcode commented on December 10, 2024

This also seems to be a problem when using formAction on a button inside the form. If you have a button that has the formAction attribute the name and value of that button are not surfaced in the form data reguardless of whether the button is inside the form or outside the form.

Code example comparing a "default" submit button (which does surface the button's form data properly) and a button with the formAction attribute set with a client action handler: https://codesandbox.io/s/hungry-bose-j6tv7p?file=/App.js

from react.

sophiebits avatar sophiebits commented on December 10, 2024

Code pointer for debugging – it's meant to be added here:

if (submitter) {
// The submitter's value should be included in the FormData.
// It should be in the document order in the form.
// Since the FormData constructor invokes the formdata event it also
// needs to be available before that happens so after construction it's too
// late. We use a temporary fake node for the duration of this event.
// TODO: FormData takes a second argument that it's the submitter but this
// is fairly new so not all browsers support it yet. Switch to that technique
// when available.
const temp = submitter.ownerDocument.createElement('input');
temp.name = submitter.name;
temp.value = submitter.value;
(submitter.parentNode: any).insertBefore(temp, submitter);
formData = new FormData(form);
(temp.parentNode: any).removeChild(temp);

from react.

AlejandroBlanco2001 avatar AlejandroBlanco2001 commented on December 10, 2024

@mattcarrollcode I have faced this similar issue when building forms with the button outside the <form> tag and using the attribute form inside the button with React. I think that in your example works because is pure HTML, but as for as I understand this situations is due on how React handles this event, to overcome this I always use onSubmit event.

Maybe have a deeper look on the code pointed by @sophiebits would give more clarity.

from react.

lllomh avatar lllomh commented on December 10, 2024

from react.

jupapios avatar jupapios commented on December 10, 2024

I just created a PR that should fix the original issue, @mattcarrollcode regarding the second issue, it seems there is a test that expects such behavior, so I guess that, for now, it is expected, but I might be wrong, @sophiebits can you confirm, please? 🙏

from react.

lllomh avatar lllomh commented on December 10, 2024

from react.

github-actions avatar github-actions commented on December 10, 2024

This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize it yet. If you have any new additional information, please include it with your comment!

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.