{"id":21592939,"url":"https://github.com/jonabrams/signals-react-safe","last_synced_at":"2025-04-06T16:14:20.000Z","repository":{"id":205646811,"uuid":"714716940","full_name":"JonAbrams/signals-react-safe","owner":"JonAbrams","description":"A version of Preact Signals that integrates with React without touching React's internals. Allows Signals to be safely used with Next.js 13+'s App Router.","archived":false,"fork":false,"pushed_at":"2024-11-07T01:59:33.000Z","size":65,"stargazers_count":42,"open_issues_count":0,"forks_count":3,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-03-30T15:11:11.312Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/JonAbrams.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-11-05T17:09:15.000Z","updated_at":"2025-03-28T16:33:24.000Z","dependencies_parsed_at":"2024-11-18T04:40:28.251Z","dependency_job_id":"18a3a4cb-177e-426a-9aa7-46dcdd8386f8","html_url":"https://github.com/JonAbrams/signals-react-safe","commit_stats":null,"previous_names":["jonabrams/signals-react-safe"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JonAbrams%2Fsignals-react-safe","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JonAbrams%2Fsignals-react-safe/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JonAbrams%2Fsignals-react-safe/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JonAbrams%2Fsignals-react-safe/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/JonAbrams","download_url":"https://codeload.github.com/JonAbrams/signals-react-safe/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247509237,"owners_count":20950232,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-24T17:10:34.954Z","updated_at":"2025-04-06T16:14:19.974Z","avatar_url":"https://github.com/JonAbrams.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# signals-react-safe\n\n[Signals]([@preact/signals](https://github.com/preactjs/signals)) is a state management library from the Preact team. This is a _safer_ alternative library.\n\nThe team provides a react compatibility library [signals-react](https://github.com/preactjs/signals/tree/main/packages/react), but it comes with a very large downside: It alters React's internals. This is a [big no-no](https://github.com/facebook/react/issues/26704#issuecomment-1522044060) for mainting compatibility with future versions of React, creating risk for your project. It also breaks compatibility with [Next.js](https://nextjs.org/), which is a pretty good and popular React framework.\n\nThis library still lets you get the biggest benefit of Signals. When you render a signal directly within your JSX/TSX it creates a text node that updates when the signal updates, skipping the re-render of the component you render it within.\n\nThis library provides [new hooks](#api) for reading the value of a signal in your React components. Instead of reading `mySignal.value` somewhere in your component, use `useSignalValue(mySignal)` instead.\n\nIMHO, this is also helps readibility since it makes it more clear what can cause a component to re-render, and works better with hooks such as useEffect.\n\n[Live editable demo](https://codesandbox.io/s/signals-react-safe-demo-jmcwst?file=/src/Counter.tsx)\n\n## Install\n\n```bash\nnpm install signals-react-safe\n```\n\n## Usage Example\n\n`mySignals.ts`\n\n```ts\nimport { signal } from 'signals-react-safe';\n\nexport const counter = signal(0);\n```\n\n`Counter.tsx` (this component does NOT re-render when the signal updates, which is more performant)\n\n```tsx\nimport { counter } from \"./mySignals\";\n\nexport function Counter() {\n  return (\n    \u003cdiv\u003e\n      \u003cdiv\u003eCurrent Count: {counter}\u003c/div\u003e\n      \u003cbutton onClick={() =\u003e counter.value++}\u003eAdd One\u003c/button\u003e\n    \u003c/div\u003e\n  );\n}\n```\n\n`MixedCounter.tsx` (this component does re-render when the signal updates, which allows the value to be combined with non-signal values)\n\n```tsx\nimport { useState } from \"react\";\nimport { counter } from \"./mySignals\";\n\nexport function MixedCounter() {\n  const counterValue = useSignalValue(counter);\n  const [myCounter, setMyCounter] = useState(0);\n\n  return (\n    \u003cdiv\u003e\n      \u003cdiv\u003eSummed: {counterValue + myCounter}\u003c/div\u003e\n      \u003cbutton onClick={() =\u003e counter.value++}\u003eAdd One to signal counter\u003c/button\u003e\n      \u003cbutton onClick={() =\u003e setMyCounter(myCounter + 1)}\u003e\n        Add One to useState counter\n      \u003c/button\u003e\n    \u003c/div\u003e\n  );\n}\n```\n\n# API\n\nThis library provides a bunch of exported function:\n\n- Re-exports from the [core Signals library](https://github.com/preactjs/signals): `signal`, `computed`, `effect`.\n- Copy/pasted hooks from [@preact/signals-react](https://github.com/preactjs/signals/blob/main/packages/react): `useSignal`, `useComputed`, `useSignalEffect`.\n- New hooks unique to this library: `useSignalValue`, `useSignalAndValue`, `useComputedValue`.\n\n### `useSignalValue(signal)`\n\nTakes as input an existing signal, typically imported into your module from elsewhere in your app. If you want your component to create its own signal, use [`useSignal`](https://github.com/preactjs/signals/tree/main/packages/react#hooks) instead.\n\nIt returns the value stored in the signal. When the signal's value changes, this hook will trigger a component re-render and return the new value.\n\nUse this hook when you need to access the value stored in the signal, e.g. triggering a useEffect, or combining with non-signal values.\n\nIf you want to combine the value with other signal values, consider using `useComputed` or `useComputedValue` to be more performant.\n\nExample:\n```tsx\nimport {count} from '../signals';\nfunction Count() {\n  const countValue = useSignalValue(count);\n  useEffect(() =\u003e {\n    localStorage['count'] = `#{countValue}`;\n  }, [countValue]);\n  return \u003cdiv\u003e{count} \u003cbutton onClick={() =\u003e count.value++}\u003eAdd one\u003c/button\u003e\u003c/div\u003e\n}\n```\n\n### `useSignalAndValue`\n\nCreates a signal and returns it, along with its value. Triggers a component re-render in order to return the latest value. Use this to replace `useSignal` if you need to read the value of a component's signal within the component.\n\nIt returns an array with two elements:\n1. The signal.\n2. Its value.\n\nExample: \n```tsx\nfunction MyCounter() {\n  const [count, countValue] = useSignalAndValue(0);\n  useEffect(() =\u003e {\n    localStorage['count'] = `#{countValue}`;\n  }, [countValue]);\n  return \u003cdiv\u003e{count} \u003cbutton onClick={() =\u003e count.value++}\u003eAdd one\u003c/button\u003e\u003c/div\u003e;\n}\n```\n\n### `useComputedValue`\n\nTake a function as its parameter and returns a value. It's useful when that function uses multiple signals. \n\nA new value will be generated if any of the referenced signals are updated, causing the component to re-render.\n\nExample:\n```tsx\nimport {count} from '../signals';\nfunction MyMultiplier() {\n  const multiplier = useSignal(1);\n  const multipliedValue = useComputedValue(() =\u003e multiplier.value * count.value);\n  useEffect(() =\u003e {\n    localStorage['multiplied'] = `#{multipliedValue}`;\n  }, [countValue]);\n  return \u003cdiv\u003e{multiplier} \u003cbutton onClick={() =\u003e multiplier.value++}\u003eAdd one\u003c/button\u003e\u003c/div\u003e;\n}\n```\n\n# Performance\n\nThe new hooks that directly return values will trigger re-renders of the component. This would also happen if you read a signal's value inside a component when using [@preact/signals-react](https://github.com/preactjs/signals/tree/main/packages/react). If you want to avoid that re-render, place your signal directly in your JSX.\n\n# Author\n\nCreated by [Jon Abrams](https://threads.net/jon.abrams) (2023)\n\n# Attributions\n\nContains code from [Preact Signals](https://github.com/preactjs/signals)\n\nIdea inspired by [satoshi-cyber](https://github.com/satoshi-cyber)'s [suggestion](https://github.com/vercel/next.js/issues/45054#issuecomment-1694791734).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjonabrams%2Fsignals-react-safe","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjonabrams%2Fsignals-react-safe","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjonabrams%2Fsignals-react-safe/lists"}