Skip to content

Commit b4c6de9

Browse files
committed
docs: state management
1 parent e5f8f02 commit b4c6de9

File tree

5 files changed

+94
-4
lines changed

5 files changed

+94
-4
lines changed

.changeset/curly-badgers-drum.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"docs": patch
3+
---
4+
5+
docs: state management

.changeset/fast-wasps-yawn.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"framesjs-starter": patch
3+
---
4+
5+
fix: state management generic type demo
+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
---
2+
title: "Guide: Frames.js State"
3+
description: "Frames.js is the react based framework for making frames. Debugger included."
4+
---
5+
6+
# Guide: State Management in Frames.js
7+
8+
State in Frames.js is a way for you to store data that you want to use in between frames requests. The frames spec allows up to 4kb of data to be stored in the state object.
9+
10+
## Setup
11+
12+
To use state in frames.js, you should declare the initial state in your `createFrames` call. If your state type is not declared explicitly it will be derived from the initial state.
13+
14+
```ts [frames.ts]
15+
import { createFrames } from "@framesjs/next";
16+
17+
export type State = {
18+
count: number;
19+
};
20+
21+
export const frames = createFrames<State>({
22+
initialState: {
23+
count: 0,
24+
},
25+
});
26+
```
27+
28+
## Accessing state
29+
30+
You can access the state object in your frame on the `ctx` parameter. The initial frame will have the state object with the initial values.
31+
32+
```ts [route.tsx]
33+
import { frames } from "./frames";
34+
35+
const handler = frames(async ({ ctx }) => {
36+
return {
37+
image: <div tw="flex">Count: {ctx.state.count}</div>, // [!code focus]
38+
};
39+
});
40+
41+
export const GET = handler;
42+
export const POST = handler;
43+
```
44+
45+
## Updating state
46+
47+
To update the state, you just include the updated state object in your handler's [`FrameDefinition`](/reference/core/createFrames#framedefinition) return value.
48+
49+
```ts [route.tsx]
50+
import { frames, Button } from "./frames";
51+
52+
const handler = frames(async ({ ctx }) => {
53+
const currentState = ctx.state;
54+
55+
// Update the state // [!code focus]
56+
const updatedState = { // [!code focus]
57+
...currentState, // [!code focus]
58+
count: currentState.count + 1, // [!code focus]
59+
}; // [!code focus]
60+
61+
return {
62+
image: <div tw="flex">Count: {updatedState.count}`</div>, // [!code focus],
63+
buttons: [
64+
<Button action="post">Increment</Button>
65+
],
66+
state: updatedState, // [!code focus]
67+
};
68+
});
69+
70+
export const GET = handler;
71+
export const POST = handler;
72+
```

docs/vocs.config.tsx

+7-3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ const sidebar = [
2222
text: "Multi-page Frames",
2323
link: "/guides/multiple-frames",
2424
},
25+
{
26+
text: "State Management",
27+
link: "/guides/state-management",
28+
},
2529
{
2630
text: "Deploying your Frame",
2731
link: "/guides/deployment",
@@ -87,9 +91,9 @@ const sidebar = [
8791
link: "/reference/core/remix",
8892
},
8993
{
90-
text: 'Cloudflare Workers',
91-
link: '/reference/core/cloudflare-workers'
92-
}
94+
text: "Cloudflare Workers",
95+
link: "/reference/core/cloudflare-workers",
96+
},
9397
],
9498
},
9599
{
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { createFrames } from "frames.js/next";
22

3-
export const frames = createFrames({
3+
type State = {
4+
counter: number;
5+
};
6+
7+
export const frames = createFrames<State>({
48
basePath: "/frames",
59
initialState: { counter: 0 },
610
});

0 commit comments

Comments
 (0)