I have a redux-form inside a component "MyEditDataForm" and I am passing a function for handling submit to it like this:
const myHandleSubmit = ({ firstInputFieldName, secondInputFieldName }) => {
console.log(firstInputFieldName);
console.log(secondInputFieldName);
};
const EditDataPage = () => {
return (
<Container>
<Row>
<MyEditDataForm
onSubmit={myHandleSubmit}
/>
</Row>
</Container>
);
};
Console output: "john", "doe"
So far so good... Now I would like to add a parameter to myHandleSubmit, which I want to pass to myHandleSubmit. Like this:
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>
);
};
Output before clicking the submit button: undefined, undefined, "some info"
After clicking the button: "Error: You must either pass handleSubmit() an onSubmit function or pass onSubmit as a prop"
How is it possible to pass aNewParameter to myHandleSubmit, without destroying the form submit functionality? I have tried to do this with bind():
<MyEditDataForm
onSubmit={myHandleSubmit.bind({ anotherParameter: aNewParameter })}
/>
Output: "john", "doe", undefined
What am I doing wrong?
CodePudding user response:
Probably you need this:
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>
);
};