Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

172
Views
Pase una función con un parámetro predefinido a handleSubmit en React

Tengo un formulario redux dentro de un componente "MyEditDataForm" y estoy pasando una función para manejar el envío de esta manera:

 const myHandleSubmit = ({ firstInputFieldName, secondInputFieldName }) => { console.log(firstInputFieldName); console.log(secondInputFieldName); }; const EditDataPage = () => { return ( <Container> <Row> <MyEditDataForm onSubmit={myHandleSubmit} /> </Row> </Container> ); };

Salida de consola: "john", "doe"

Hasta ahora todo bien... Ahora me gustaría agregar un parámetro a myHandleSubmit, que quiero pasar a myHandleSubmit. Como esto:

 const myHandleSubmit = ({ firstInputFieldName, secondInputFieldName, anotherParameter }) => { console.log(firstInputFieldName); console.log(secondInputFieldName); console.log(anotherParameter); }; const EditDataPage = () => { const aNewParameter = "some info"; return ( <Container> <Row> <MyEditDataForm onSubmit={myHandleSubmit({ anotherParameter: aNewParameter })} /> </Row> </Container> ); };

Salida antes de hacer clic en el botón Enviar: indefinido, indefinido, "alguna información"

Después de hacer clic en el botón: "Error: debe pasar handleSubmit() una función onSubmit o pasar onSubmit como accesorio"

¿Cómo es posible pasar un NewParameter a myHandleSubmit, sin destruir la funcionalidad de envío de formulario? He intentado hacer esto con bind():

 <MyEditDataForm onSubmit={myHandleSubmit.bind({ anotherParameter: aNewParameter })} />

Salida: "john", "doe", indefinido

¿Qué estoy haciendo mal?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Probablemente necesites esto:

 const myHandleSubmit = ({ firstInputFieldName, secondInputFieldName, anotherParameter }) => { console.log(firstInputFieldName); console.log(secondInputFieldName); console.log(anotherParameter); }; const EditDataPage = () => { const aNewParameter = "some info"; return ( <Container> <Row> <MyEditDataForm onSubmit={(fields) => myHandleSubmit({ ...fields, anotherParameter: aNewParameter })} /> </Row> </Container> ); };
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!