简体   繁体   中英

Why doesn't redux-form pass in the value from an option field as prop in the submit function?

The Problem

I have 2 simple redux form's. They consist title and subTitle fields of type text .

One of the two forms uses another field type select . I pass in an option like this:

<Field name="test" component="select">
        <option value="test">test</option>
</Field>

So I fill out the form and console log the props like this:

The general Form submit: <Form onSubmit={handleSubmit(submitTopic)}>

The actual function that runs:

const submitTopic = props => {
    console.log(props);
    // dispatch(createTopic(test, topicTitle, topicSubTitle));
};

So I expect to see the title as the string I typed in in the form and the subTitle as the string I typed in. (works for both forms) I also expect to see a third property called "test" with the string "test" as value in the second form.

But it's actually undefined

The Code

Here is the link to my repo:

https://github.com/dhuber666/CodeHUB

The files where all this happens are here:

在此处输入图片说明

The Admin.js file is where I import it and run it.

I made sure that each form has it's unique identifier when using the reduxForm helper method.

A minimal Example to test out on Codesandbox

https://codesandbox.io/s/w0mqnwqz9w

Here I recreated the app but just with the bare mininum to test this out. And I have the same problem here. I don't get the option value passed in into props.

The console.log for the props is in the file Form at line number 10

Thank you for your help!

It works when you try to select an option, it doesn't work when you directly submit without selecting because the value is undefined initially until onChange event is triggered. if you want to skip that, You can provide a default value by creating a wrapper component for select.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM