0.1.10 • Published 1 year ago

@strozw/use-action-state-compat v0.1.10

Weekly downloads
-
License
MIT
Repository
-
Last release
1 year ago

useActionStateCompat

Next.js v14 and React.js v18 canary compatible useActionState

Installation

npm install @strozw/use-action-state-compat

Usage

import { useActionStateCompat } from "@strozw/use-action-state-compat";

type FormState = null | string;

const myAction = async (_state: FormState, data: FormData) => {
  return new Promise<null | string>((resolve) => {
    window.setTimeout(() => {
      if (!data.get("word")) {
        resolve("please input `word`");
      }
      resolve(null);
    }, 3000);
  });
};

export function MyFrom() {
  const [currentState, action, isPending] = useActionStateCompat(
    myAction,
    null,
  );

  return (
    <div>
      {currentState && <div>ERROR: {currentState}</div>}
      <form action={action}>
        <input type="text" name="word" />
        <button type="submit" disabled={isPending}>
          Submit
        </button>
      </form>
    </div>
  );
}
0.1.10

1 year ago

0.1.9

1 year ago

0.1.8

1 year ago

0.1.7

1 year ago

0.1.6

1 year ago

0.1.5

1 year ago

0.1.3

1 year ago

0.1.2

1 year ago

0.1.1

1 year ago

0.1.0

1 year ago