Basic conceptsHistory

Basic concepts

History

Learn how the history prop controls back navigation in the form.


History

Pass history to the Formity component or the useFormity hook to control whether Formity tracks the steps the user has visited. It is a boolean that defaults to true.

When it is true, back returns to the step the user was on before, including the step they jumped from. This is the right choice when users move through the form with next and back.

When it is false, only the current step is kept, so back stays on the current step. Use it when navigation is handled with the jump element instead, as in the following example.

TSX
import { useCallback, type ReactNode } from "react";

import { Formity, type s, type Flow, type OnReturn } from "@formity/react";

import { NameForm } from "./components/name-form";
import { EmailForm } from "./components/email-form";

type Schema = {
  render: ReactNode;
  struct: [
    s.Jump<s.Form<{ name: string }>>,
    s.Jump<s.Form<{ email: string }>>,
    s.Return<{ name: string; email: string }>,
  ];
  inputs: {
    name: string;
    email: string;
  };
  params: Record<never, never>;
};

const flow: Flow<Schema> = [
  {
    jump: {
      id: "name",
      at: {
        form: {
          fields: ({ name }) => ({ name: [name, []] }),
          render: ({ fields, next, jump }) => (
            <NameForm fields={fields} next={next} jump={jump} />
          ),
        },
      },
    },
  },
  {
    jump: {
      id: "email",
      at: {
        form: {
          fields: ({ email }) => ({ email: [email, []] }),
          render: ({ fields, next, jump }) => (
            <EmailForm fields={fields} next={next} jump={jump} />
          ),
        },
      },
    },
  },
  {
    return: ({ name, email }) => ({ name, email }),
  },
];

export default function App() {
  const onReturn = useCallback<OnReturn<Schema>>((output) => {
    // {
    //   name: string;
    //   email: string;
    // }
    console.log(output);
  }, []);

  return (
    <Formity<Schema>
      flow={flow}
      inputs={{ name: "", email: "" }}
      history={false}
      onReturn={onReturn}
    />
  );
}