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.

[Preact, RHF 7.0] Radio inputs not working

See original GitHub issue

Describe the bug Radio inputs aren’t working correctly in Preact project after upgrading to the latest RHF version. Only last selected input produces a value, other input values are shown as null.

To Reproduce Steps to reproduce the behavior:

  1. Go to the Codesandbox example
  2. Click on ‘One’, click ‘Submit’
  3. {test: null} is logged to the console
  4. Click on ‘Three’, click ‘Submit’
  5. {test: "three"} is logged to the console

Codesandbox link (Required) https://codesandbox.io/s/funny-saha-1dr1e?file=/src/index.js

Expected behavior All radio inputs produce values.

Screenshots N/A

Desktop:

  • OS: Windows
  • Browser: Edge
  • Version: 89

Additional context Not sure if Preact compatibility is something that has any priority, just wanted to report this as it worked in RHF 6.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

6reactions
leapfulcommented, Apr 4, 2021

@r04423

There is an use case to show/hide input element that needs “document.contains”, while the “detached” DOM element and “not attached yet” DOM element have the same behavior, it’s hard to define a handling for use case of Preact.

I would suggest another workaround to keep using “register” instead of using Controller:

const { register } = useForm();

const customRegister = (name, options) => {
  const { ref, ...field } = register(name, options);
  
  const customRef = (...args) => {
    setTimeout(() => ref(...args));
  }
  
  return { ...field, ref: customRef };
}

// Just use "customRegister" instead of "register"

<input {...customRegister('test')} id="one" />
<input {...customRegister('test')} id="two" />
<input {...customRegister('test') id="three" }/>
1reaction
r04423commented, Apr 4, 2021

@leapful and @bluebill1049 thank you so much for looking into this!

And indeed, this workaround does seem to work 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

Radio - preact-material-components
This is a possible example of controlling the values of a group of radio buttons in component state. import { Component } from...
Read more >
vpzomtrrfrt/preact-radio-group: Better radio buttons. - GitHub
A few problems: Repetitive fields ( name , type , checked , onChange ). Hard to set the checked value. You need to...
Read more >
Radio buttons and checkboxes won't work with Contact form 7 ...
If I insert my form with Contact form 7 Styler my checkboxes and radio buttons don't work. If I insert it with the...
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