fungible-systems / micro-stacks

Tiny libraries for building the next generation of Stacks apps.
https://micro-stacks.dev
MIT License
82 stars 15 forks source link

fix: make button text reactive #139

Closed jjenzz closed 2 years ago

jjenzz commented 2 years ago

as i'm sure you already know, components in solidjs only execute once so derived values have to execute in the JSX or use createMemo to track the signal.

i went with moving the logic into the JSX but either of these would have also worked:

export const NetworkToggle = () => {
  const { isMainnet, setNetwork } = useNetwork();
-  const networkMode = isMainnet() ? 'mainnet' : 'testnet';
+  const networkMode = () => isMainnet() ? 'mainnet' : 'testnet';
  return (
-    <button onClick={() => setNetwork(isMainnet() ? 'testnet' : 'mainnet')}>{networkMode}</button>
+    <button onClick={() => setNetwork(isMainnet() ? 'testnet' : 'mainnet')}>{networkMode()}</button>
  );
};
+ import { createMemo } from 'solid-js';

export const NetworkToggle = () => {
  const { isMainnet, setNetwork } = useNetwork();
-  const networkMode = isMainnet() ? 'mainnet' : 'testnet';
+  const networkMode = createMemo(() => isMainnet() ? 'mainnet' : 'testnet');
  return (
    <button onClick={() => setNetwork(isMainnet() ? 'testnet' : 'mainnet')}>{networkMode}</button>
  );
};

let me know which approach you'd prefer :)

changeset-bot[bot] commented 2 years ago

⚠️ No Changeset found

Latest commit: 1a1f5bf66714279d55084dcbf8eb8c909f856cfb

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

vercel[bot] commented 2 years ago

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated
micro-stacks-docs ✅ Ready (Inspect) Visit Preview Jul 28, 2022 at 3:40PM (UTC)