百科.dev
全部条目AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
E

effect-atom

> 编程语言
开源

@effect-atom/atom 一个用于 Effect 的反应式状态管理库。 安装 如果您使用的是 React: bash pnpm add @effect-atom/atom-react

770 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

@effect-atom/atom 一个用于 Effect 的反应式状态管理库。 安装 如果您使用的是 React: bash pnpm add @effect-atom/atom-react

@effect-atom/atom

A reactive state management library for Effect.

Installation

If you are using React:

bash
pnpm add @effect-atom/atom-react

Creating a Counter with Atom

Let's create a simple Counter component, which will increment a number when you click a button.

We will use Atom.make to create our Atom, which is a reactive state container.

We can then use the useAtomValue & useAtomSet hooks to read and update the value of the Atom.

…

Derived State

You can create derived state from an Atom in a couple of ways.

typescript
import { Atom } from "@effect-atom/atom-react"

const countAtom = Atom.make(0)

// You can use the `get` function to get the value of another Atom.
//
// The type of `get` is `Atom.Context`, which also has a bunch of other methods
// on it to manage Atoms.
//
const doubleCountAtom = Atom.make((get) => get(countAtom) * 2)

// You can also use the `Atom.map` function to create a derived Atom.
const tripleCountAtom = Atom.map(countAtom, (count) => count * 3)

Working with Effects

You can also pass effects to the Atom.make function.

When working with effectful Atoms, you will get back a Result type.

You can see all the ways to work with Result here: https://tim-smart.github.io/effect-atom/atom/Result.ts.html

typescript
import { Atom, Result } from "@effect-atom/atom-react"
import { Effect } from "effect"

//        ┌─── Atom.Atom>
//        ▼
const countAtom = Atom.make(Effect.succeed(0))

// You can also pass a function to get access to the `Atom.Context`
//
// `get.result` can be used in `Effect`s to get the value of an `Atom.Atom`.
//
//             ┌─── Atom.Atom>
//             ▼
const resultWithContextAtom = Atom.make(
  Effect.fnUntraced(function* (get: Atom.Context) {
    const count = yield* get.result(countAtom)
    return count + 1
  }),
)

Working with scoped Effects

All Atoms that use effects are provided with a Scope, so you can add finalizers that will be run when the Atom is no longer used.

typescript
import { Atom } from "@effect-atom/atom-react"
import { Effect } from "effect"

const resultAtom = Atom.make(
  Effect.gen(function* () {
    // Add a finalizer to the `Scope` for this Atom
    // It will run when the Atom is rebuilt or no longer needed
    yield* Effect.addFinalizer(() => Effect.log("finalizer"))
    return "hello"
  }),
)

Working with Effect Services / Layers

…

Adding global Layers to AtomRuntimes

This is useful for setting up Tracers, Loggers, ConfigProviders, etc.

typescript
import { Atom } from "@effect-atom/atom-react"
import { ConfigProvider, Layer } from "effect"

Atom.runtime.addGlobalLayer(
  Layer.setConfigProvider(ConfigProvider.fromJson(import.meta.env)),
)

Working with Streams

…

Working with sets of Atoms

…

Working with functions

…

Wrapping an event listener

…

Integration with search params

typescript
import { Atom } from "@effect-atom/atom-react"
import { Option, Schema } from "effect"

// Create an Atom that reads and writes to the URL search parameters.
//
//          ┌─── Atom.Writable
//          ▼
const simpleParamAtom = Atom.searchParam("paramName")

// You can also use a schema to further parse the value
//
//          ┌─── Atom.Writable>
//          ▼
const numberParamAtom = Atom.searchParam("paramName", {
  schema: Schema.NumberFromString,
})

Integration with local storage

typescript
import { Atom } from "@effect-atom/atom-react"
import { BrowserKeyValueStore } from "@effect/platform-browser"
import { Schema } from "effect"

const runtime = Atom.runtime(BrowserKeyValueStore.layerLocalStorage)

// Create an Atom that reads and writes to `localStorage`.
//
// It uses `Schema` to define the type of the value stored.
//
//       ┌─── Atom.Writable
//       ▼
const flagAtom = Atom.kvs({
  runtime: runtime,
  key: "flag",
  schema: Schema.Boolean,
  defaultValue: () => false,
})

Integration with Reactivity from @effect/experimental

Reactivity is an Effect service that allows you make queries reactive when mutations happen.

You can use an Atom.runtime to hook into the Reactivity service and trigger Atom refreshes when mutations happen.

…

@effect/rpc integration

You can use the AtomRpc module to create an RPC client with integration with effect-atom. It offers apis for both queries and mutations.

…

HttpApi integration

You can use the AtomHttpApi module to create an HTTP API client with integration with effect-atom. It offers apis for both queries and mutations.

…

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

TypeScript

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言