question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

FormSection and nested Field component bug

See original GitHub issue

Are you submitting a bug report or a feature request?

Bug with FormSection and nested Field

What is the current behavior?

When using FormSection with nested Field components, input names get prefix with every nested reuse.


const fieldComponent = ({input}) => {
	console.log(input.name); // as expected form1.foo
        return <Field {...input} component={nextFieldComponent} />
}

const nextFieldComponent = ({input}) => {
	console.log(input.name); // becomes form1.foo.foo
	return <input {...input} />
}

const Form = () => (
  <div>
    <FormSection name='form1'>
      <Field name='foo' component={fieldComponent} />
    </FormSection>    
  </div>
)

fiddle with alert on this: https://jsfiddle.net/86v1yuuz/2/

What is the expected behavior?

It should not append FormSection name on nested Field components

Sandbox Link

https://jsfiddle.net/86v1yuuz/2/

What’s your environment?

Redux v. 6.6.3, React 15.5.3

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:20 (17 by maintainers)

github_iconTop GitHub Comments

2reactions
wmertenscommented, May 31, 2017

BTW @Kestutis I think you can solve your problem as follows:

const FieldComponent = ({name}) => {
	console.log(name); // as expected foo
        return <Field name={name} component={NextFieldComponent} />
}

const NextFieldComponent = ({input}) => {
	console.log(input.name); // becomes form1.foo
	return <input {...input} />
}

const Form = () => (
  <div>
    <FormSection name='form1'>
      <FieldComponent name='foo'/>
    </FormSection>    
  </div>
)

Try to make your forms so that no matter what the structure is, the top component is the one that has @reduxForm, all intermediate components use only <FormSection> if they need to work on subtrees, and the <Field ...> components are at the bottom of your structure.

Also, React components need to be uppercase, otherwise JSX won’t recognize them as components.

0reactions
lock[bot]commented, Jun 9, 2018

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Redux forms nested initialValues - reactjs - Stack Overflow
My field names is contents.ru.text, contents.ru.title and etc. My main point is i never get the value in my input components but initialValue...
Read more >
Best practices for handling nested forms?
If i am including pages like this iam getting an error saying "Error: 'apex:form' component cannot be nested within form tags".
Read more >
FieldArray - Redux Form
The FieldArray component is how you render an array of fields. ... When nested in FormSection , returns the name prop prefixed with...
Read more >
react hook form nested | The AI Search Engine You Control
React Hook Form: Submit a form with nested components or extract fields of ... I have no idea why it happens though, it...
Read more >
How to structure a web form - Learn web development | MDN
Nesting can cause forms to behave unpredictably, so it is a bad idea. ... <legend> element as if it is a part of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found