Nesting conditional doen't work
See original GitHub issueDescribe the bug Conditional work only one level but you nest conditional i.e. add conditional that dependends on conditional just 2 leves then it gets messed up.
To Reproduce
- Click on Add Todo button
- Select either Type One or Type Two
- Now Select either Sub Type.
Issue here is
- None of the subtype radio is getting selected.
- Subtype select first time works but if you switch them then it doesn’t work.
import { ArrayField, useFieldArray, useForm } from "react-hook-form"
interface Props {}
interface ConProps {
index: number
field: Partial<ArrayField<Record<string, any>, "id">>
}
function TestTwo(props: Props): JSX.Element {
const { register, handleSubmit, control, watch } = useForm()
const { fields, append, remove } = useFieldArray({
control,
name: "todos",
})
function onSubmit(input: any) {
console.log(input)
}
function NestedConditional({ field, index }: ConProps): JSX.Element | null {
const todoSubType = watch(`todos[${index}].todoSubType`) as string | undefined
if (todoSubType) {
if (todoSubType === "subType1") {
return (
<div>
<label htmlFor="finalInput">Final Input</label>
<input type="text" name={`todos[${index}].nameOne`} ref={register} defaultValue={field.nameOne} />
</div>
)
} else if (todoSubType === "subType2") {
return (
<div>
<label htmlFor="finalInput">Final Input</label>
<input type="text" name={`todos[${index}].nameTwo`} ref={register} defaultValue={field.nameTwo} />
</div>
)
} else if (todoSubType === "subType3") {
return (
<div>
<label htmlFor="finalInput">Final Input</label>
<input type="text" name={`todos[${index}].nameThree`} ref={register} defaultValue={field.nameThree} />
</div>
)
} else if (todoSubType === "subType4") {
return (
<div>
<label htmlFor="finalInput">Final Input</label>
<input type="text" name={`todos[${index}].nameFour`} ref={register} defaultValue={field.nameFour} />
</div>
)
}
}
return null
}
function ShowConditional({ field, index }: ConProps): JSX.Element | null {
const todoType = watch(`todos[${index}].todoType`) as string | undefined
if (todoType) {
if (todoType === "type1") {
return (
<>
<fieldset>
<legend>Pick Subtype</legend>
<label>
<input type="radio" name={`todos[${index}].todoSubType`} value="subType1" ref={register} />
Sub Type 1
</label>
<label>
<input type="radio" name={`todos[${index}].todoSubType`} value="subType2" ref={register} />
Sub Type 2
</label>
</fieldset>
<NestedConditional field={field} index={index} />
</>
)
} else if (todoType === "type2") {
return (
<>
<fieldset>
<legend>Pick Subtype</legend>
<label>
<input type="radio" name={`todos[${index}].todoSubType`} value="subType3" ref={register} />
Sub Type 3
</label>
<label>
<input type="radio" name={`todos[${index}].todoSubType`} value="subType4" ref={register} />
Sub Type 4
</label>
</fieldset>
</>
)
}
}
return null
}
return (
<div>
<h1>Test two page</h1>
<form onSubmit={handleSubmit(onSubmit)}>
{fields.map((item, index) => {
return (
<div key={item.id}>
<fieldset>
<legend>Pick Type</legend>
<label>
<input type="radio" name={`todos[${index}].todoType`} value="type1" ref={register} />
Type 1
</label>
<label>
<input type="radio" name={`todos[${index}].todoType`} value="type2" ref={register} />
Type 2
</label>
</fieldset>
<ShowConditional field={item} index={index} />
<div>
<button type="button" onClick={() => remove(index)}>
Remove
</button>
</div>
</div>
)
})}
<button type="button" onClick={() => append({})}>
Add Todo
</button>
<button type="submit">Submit</button>
</form>
</div>
)
}
export default TestTwo
Codesandbox link (Required) https://codesandbox.io/s/react-hook-form-useform-template-forked-buss4?file=/src/index.js
Expected behavior I just want two level conditional form. And it should work.
Desktop (please complete the following information):
- OS: [e.g. iOS] Windows
- Browser [e.g. chrome, safari] Chrome
- Version [e.g. 22] 89
Issue Analytics
- State:
- Created 2 years ago
- Comments:8 (4 by maintainers)
Top Results From Across the Web
Nested IF ELSE condition and logic not working for some reason
Syntax and flow appears to be correct for conditions. I am interested to learn why the logic below doesn't work. Current output. File...
Read more >Nested conditionals (if/else/if) | AP CSP (article) | Khan Academy
Computer programs use conditionals to select the correct path for a program to go down. When a program only selects one of two...
Read more >Only the first IF condition works, and the rest doesn't when ...
I tried to write the formula like below and if I input (000) 000-0000 into HomePhone field, that turns into +1 000000000 successfully,...
Read more >Nesting "If Statements" Is Bad. Do This Instead. - YouTube
You should never nest if statement because it is hard to read and edit. ... read 0:50 How Guard Causes works 1:10 Reverse...
Read more >Excel Nested IF statements - examples, best practices and ...
In your first example the formula shown in screenshot doesn't meet the criteria condition mentioned in blog. Instead the formula should be like ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
If someone has the same issue I found a solution
This will help you avoid setting
register: any
and get full typesafety to catch errors faster.Also bonus you get auto complete to help you code faster.
If I had knows this then I wouldn’t have had the issue to begin with.
Sorry @bluebill1049 I created code sandbox account for first time and I spent long time writing code and I hit save but somehow the code was not saved. So I had to re write the whole thing again.