Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: state management #277

Merged
merged 1 commit into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/curly-badgers-drum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"docs": patch
---

docs: state management
5 changes: 5 additions & 0 deletions .changeset/fast-wasps-yawn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"framesjs-starter": patch
---

fix: state management generic type demo
72 changes: 72 additions & 0 deletions docs/pages/guides/state-management.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
---
title: "Guide: Frames.js State"
description: "Frames.js is the react based framework for making frames. Debugger included."
---

# Guide: State Management in Frames.js

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.

## Setup

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.

```ts [frames.ts]
import { createFrames } from "@framesjs/next";

export type State = {
count: number;
};

export const frames = createFrames<State>({
initialState: {
count: 0,
},
});
```

## Accessing state

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.

```ts [route.tsx]
import { frames } from "./frames";

const handler = frames(async ({ ctx }) => {
return {
image: <div tw="flex">Count: {ctx.state.count}</div>, // [!code focus]
};
});

export const GET = handler;
export const POST = handler;
```

## Updating state

To update the state, you just include the updated state object in your handler's [`FrameDefinition`](/reference/core/createFrames#framedefinition) return value.

```ts [route.tsx]
import { frames, Button } from "./frames";

const handler = frames(async ({ ctx }) => {
const currentState = ctx.state;

// Update the state // [!code focus]
const updatedState = { // [!code focus]
...currentState, // [!code focus]
count: currentState.count + 1, // [!code focus]
}; // [!code focus]

return {
image: <div tw="flex">Count: {updatedState.count}`</div>, // [!code focus],
buttons: [
<Button action="post">Increment</Button>
],
state: updatedState, // [!code focus]
};
});
export const GET = handler;
export const POST = handler;
```
10 changes: 7 additions & 3 deletions docs/vocs.config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ const sidebar = [
text: "Multi-page Frames",
link: "/guides/multiple-frames",
},
{
text: "State Management",
link: "/guides/state-management",
},
{
text: "Deploying your Frame",
link: "/guides/deployment",
Expand Down Expand Up @@ -87,9 +91,9 @@ const sidebar = [
link: "/reference/core/remix",
},
{
text: 'Cloudflare Workers',
link: '/reference/core/cloudflare-workers'
}
text: "Cloudflare Workers",
link: "/reference/core/cloudflare-workers",
},
],
},
{
Expand Down
6 changes: 5 additions & 1 deletion examples/framesjs-starter/app/frames/frames.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { createFrames } from "frames.js/next";

export const frames = createFrames({
type State = {
counter: number;
};

export const frames = createFrames<State>({
basePath: "/frames",
initialState: { counter: 0 },
});
Loading