Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

soundEnabled doesn't change on boolean useState change #132

Open
EthanBlockson opened this issue Jun 12, 2023 · 0 comments
Open

soundEnabled doesn't change on boolean useState change #132

EthanBlockson opened this issue Jun 12, 2023 · 0 comments

Comments

@EthanBlockson
Copy link

EthanBlockson commented Jun 12, 2023

When I import this component into any other document, the boolean toggle works fine, but it doesn't affect soundEnabled. I have also tried name useState const as soundEnabled and passing it as { soundEnabled }.

The goal is to mute sounds onClick in the imported component.

export const useSoundPlayer = () => {
  // React states
  const [isSoundOn, setSounds] = useState(true); 
  const [hover, setHover] = useState(false);

  // Sound library
  const [playSound1] = useSound("/audio/VM_Button_Press.mp3", {
    soundEnabled: isSoundOn,
  });

  const [playSound2] = useSound("/audio/MoreTickets.mp3", {
    soundEnabled: isSoundOn,
  });

  // Turn sounds on/off
  const muteSounds = () => {
    setSounds(!isSoundOn);
    console.log(isSoundOn); // TEMP
  };

  // Change button style on hover
  const handleMouseEnter = () => {
    setHover(true);
    console.log(isSoundOn); // TEMP
  };

  const handleMouseLeave = () => {
    setHover(false);
  };

  return {
    playSound1,
    playSound2,
    soundPlayerButton: (
      <>
        <Image
          src={
            isSoundOn
              ? hover
                ? SoundOnIconWhite
                : SoundOnIconBlack
              : hover
              ? SoundOffIconWhite
              : SoundOffIconBlack
          }
          onClick={muteSounds}
          onMouseEnter={handleMouseEnter}
          onMouseLeave={handleMouseLeave}
          className={`settings-button ${hover ? "" : "off"}`}
          alt=""
        />
      </>
    ),
  };
};

export default useSoundPlayer;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant