Example 2 (with persist enabled)
In this example we have a counter state, The persist option is enabled persist: true
, meaning the state will be preserved even after you refresh or navigate out of this page
Server Component
Counter: 0
Client Component
Counter: 0
Client component with server actions
Counter: 0
1import {2 createServerState,3 getServerState,4 useServerState5} from 'next-server-state';67type MyStateProps = {8 counter: number;9};1011const myState =12 createServerState<MyStateProps>('example-two', {13 counter: 014 },15 {16 persist: true // <-- enables persist data17 });1819function useMyServerState() {20 return useServerState<MyStateProps>(myState);21}22async function getMyServerState() {23 return getServerState<MyStateProps>(myState);24}2526export myState, useMyServerState, getMyServerState;27