Hacker News new | ask | show | jobs
by AlexErrant 17 days ago
I assume you mean `useEffect`.

That does not work because `loadTTS` is actually a hook, so its `useLoadTTS`, and hooks must be top level. I encourage you to look at the pastebin in my link, repeated here https://pastebin.com/sjc3vXTd

So, I must have

    let provider = useGetUserPreference()
    let a = useLoadTTS_A()
    let b = useLoadTTS_B()
    let c = useLoadTTS_C()
    useEffect(() => {
      if (provider=="a") a()
      if (provider=="b") b()
      if (provider=="c") c()
    })
Oh hey, what's that? I have to allocate memory for b and c even though only a is used? THANKS REACT.
2 comments

Can be simplified a little bit.

  const players = {
    sherpa: SherpaPlayer,
    local: LocalPlayer,
    gemini: GeminiPlayer,
  } satisfies Record<VoiceProvider, React.ComponentType>;
  
  function PlayerSwitcher() {
    const provider = useAppStore.use.voiceProvider();
    const Player = players[provider];
    return <Player />;
  }
  
  function useRegisterPlayer(player: Player, shortPlayer: Player) {
    const { setPlayer, setShortPlayer } = useAppActions();
  
    useEffect(() => {
      setPlayer(player);
      setShortPlayer(shortPlayer);
    }, [player, shortPlayer, setPlayer, setShortPlayer]);
  }
  
  function LocalPlayer() {
    useRegisterPlayer(useLocalPlayer(), useShortLocalPlayer());
    return null;
  }
  
  function GeminiPlayer() {
    useRegisterPlayer(useGeminiPlayer(), useShortGeminiPlayer());
    return null;
  }
  
  function SherpaPlayer() {
    useRegisterPlayer(useSherpaPlayer(), useShortSherpaPlayer());
    return null;
  }
It sounds like `useLoadTTS` shouldn't have been a hook then? You don't have to make everything a hook just because they exist. Hooks are just one tool in the toolbox. There's no shame in a useState or useEffect.
Tell that to the services that only offer their APIs as a hook.

Trust me, I hate hooks. I don't use them unless I must.