Skip to content
This repository has been archived by the owner on Apr 25, 2024. It is now read-only.

Radios/Checkboxes: error in console due to null ref prior to render #132

Closed
stachow opened this issue Dec 17, 2021 · 7 comments
Closed

Radios/Checkboxes: error in console due to null ref prior to render #132

stachow opened this issue Dec 17, 2021 · 7 comments
Labels
bug Something isn't working severity: minor

Comments

@stachow
Copy link

stachow commented Dec 17, 2021

In all other similar instances in the codebase a check is made that the ref is not null before initialising the underlying govuk-frontend JS, e.g.

if (accordionRef.current) {
  new AccordionJS(accordionRef.current).init();
}

The null check is missing in the following two places, giving errors in the console when the code runs before first render:

new RadiosJS(controlRef.current).init();

new CheckboxesJS(controlRef.current).init();

@andymantell
Copy link
Collaborator

Thanks @stachow - I will take a look at this soon. Are you managing ok for a second if I don't get to this for a few days?

@andymantell andymantell added bug Something isn't working severity: minor labels Dec 17, 2021
@andymantell
Copy link
Collaborator

Interesting, this doesn't seem to happen for me. Would you be able to put together a minimal reproduction if it's not too much trouble? No worries if not, but it might speed up a fix if you are able to.

@andymantell
Copy link
Collaborator

are you doing something like initialising this component without any items, but then loading in the items later e.g. as the result of an async call? So you get that first empty render happening without any checkboxes present?

@stachow
Copy link
Author

stachow commented Dec 20, 2021

Cheers for the response. This one is very edge-casey. Due to a bug in my code, the radios are put in to the DOM on page load, then instantly removed (thereby upsetting the internal JS leading to the console error). A repro is below. This is no longer an issue for me as the fix in my code prevents this initial "micro" rendering of the radios.

import { Radios } from "govuk-react-jsx";
import { useEffect, useState } from "react";

function App() {
  const [status, setStatus] = useState("idle");

  useEffect(() => {
    if (status === "idle") {
      // For error to appear in console, this delay must be tiny. If >=20ms on my machine, 
      //  then no error.
      setTimeout(() => setStatus("loading"), 5);
    }
    if (status === "loading") {
      setTimeout(() => setStatus("loaded"), 300);
    }
  }, [status]);

  return (
    <div className="App">
      {
        /* 
        Bug in my code on the following line: 
        - lifecycle of status is "idle" -> "loading" -> "loaded"
        - following check should be status === "loaded" to prevent radios being rendered 
           when status === "idle", then whipped out of DOM when "loading" (leading to console error)  
           then reinserted when "loaded"
        */
        status !== "loading" && (
          <Radios
            name="r"
            items={[{ reactListKey: "1", value: "1", children: "foo" }]}
            onChange={() => {}}
          ></Radios>
        )
      }
    </div>
  );
}

export default App;

@stachow
Copy link
Author

stachow commented Dec 20, 2021

BTW - as you may have guessed we are investigating using your library (nice work!), if you search for me on the gov digital slack you'll see which department. Cheers

@andymantell
Copy link
Collaborator

ah thanks for explaining the scenario @stachow. Glad that you've fixed around it for now, but I agree, it would be nice if these components were able to handle such a situation. Should be an easy enough fix - I'll try and get it into the next release when I do the govuk-frontend@4.x upgrade.

@andymantell
Copy link
Collaborator

Hi @stachow. This is now fixed in govuk-react-jsx@7.0.0

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working severity: minor
Projects
None yet
Development

No branches or pull requests

2 participants