jlengstorf/honkify-demo

React example does not use best practices

Open

#3 opened on Oct 9, 2019

View on GitHub
 (1 comment) (0 reactions) (0 assignees)JavaScript (2 forks)github user discovery
good first issuehacktoberfesthelp wanted

Repository metrics

Stars
 (4 stars)
PR merge metrics
 (No merged PRs in 30d)

Description

There is a potential bug with the React example. If the Honker is unmounted from the page, honkify is not unregistered. To solve this, the example should utilize the useEffect hook:

import React, { useEffect, useState } from "react";
import honkify from "honkify";

const Honker = () => {
  const [isActive, setActive] = useState(false);

  useEffect(() => {
    if (isActive) {
      const unregister = honkify();
      return () => unregister();
    }
  }, [isActive]);

  const toggleHonk = () => {
    setActive(isActive => !isActive);
  };

  return (
    <button onClick={toggleHonk} className="no-honk">
      {isActive ? "unhonk" : "honkify!"}
    </button>
  );
};

export default Honker;

In the useEffect, unregister will be called if the isActive state is false or if the Honker is unmounted.

Contributor guide