Skip to content

Instantly share code, notes, and snippets.

@jide
jide / auto-reload.jsx
Created January 11, 2024 15:57
Auto reload for Next.js
"use client";
import useInterval from "use-interval";
import { useRouter } from "next/navigation";
export function AutoReload({ delay = 1000 }) {
const router = useRouter();
useInterval(() => {
router.refresh();
@jide
jide / PostForm.jsx
Created December 5, 2023 16:09
Next JS toast actions messages
"use client";
import { useForm } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod";
import { Form } from "@/components/ui/form";
import { Button } from "@/components/ui/button";
import * as z from "zod";
import { useFormAction } from "@/lib/useFormAction";
export function PostForm() {
@jide
jide / useDynamicMinHeight.js
Last active November 16, 2023 16:48
Prevent grid layout overlap inside flex containers.
import { useState, useLayoutEffect, useCallback } from "react";
import { flushSync } from "react-dom";
const useDynamicMinHeight = (ref) => {
const [minHeight, setMinHeight] = useState(0);
const updateMinHeight = useCallback(() => {
if (ref.current) {
flushSync(() => {
setMinHeight(0);
import React from 'react';
import { useAppState, useActions } from '~/overmind';
export default function Post() {
const { post } = useAppState();
const { myAction } = useActions();
return (
<div>
<h1>{ post.title }</h1>
import { derived } from 'overmind';
export const state = {
loading: false,
auth: {},
router: {},
post: null,
isAuthenticated: derived(state => state.auth.token !== undefined)
};
export const showPostPage = async ({ state, actions, effects }, params) => {
if (!state.auth.token) {
effects.router.open('/');
}
state.router = { page: 'post', params };
await actions.loadPost({ id: params.id });
};
export const onInitializeOvermind = ({ state, actions, effects }, instance) => {
state.auth.token = effects.auth.getLocalStorageToken();
effects.router.initialize({
'/': actions.showLoginPage,
'/:postId': actions.showPostPage
});
}
import page from 'page';
export const router = {
initialize(routes) {
Object.keys(routes).forEach(url => {
page(url, ({ params }) => routes[url](params));
});
page.start();
},
open: url => page.show(url)
  • overmind/: Group overmind stuff
    • effects/: Side effects
      • router.js: Router side effect
      • gql.js: GraphQL side effect
    • actions.js: Actions, you may want to split them into logical groups
    • state.js: The state object
  • index.js: Export state, actions, effects and hooks
import { createOvermind } from 'overmind';
import {
createStateHook,
createActionsHook,
createEffectsHook,
createReactionHook
} from 'overmind-react';
import { state } from './state';
import * as actions from './actions';
import { gql } from './effects/gql';