Skip to content

Next.js window not defined error #3

@gaurangrshah

Description

@gaurangrshah

Hey, I am trying to use formalizer in a next.js application, and I keep getting the following error:

ReferenceError: window is not defined

image

Below is a quick/truncated replication of my actual form showing a single input:

  Box,
  FormControl,
  FormLabel,
  FormErrorMessage,
  FormHelperText,
  Input,
} from "@chakra-ui/react";

import { useFormalizer } from "formalizer"; // @link https://github.com/nosachamos/formalizer
import { schema } from "@/models/schema";

export const Form = () => {
  if (!process.browser) return;
  const { formRef, useInput, errors, isValid } = useFormalizer();

  const label = "label";

  return (
    <Box as={"form"} ref={formRef}>
      <FormControl as='fieldset' isDisabled={false} isInvalid={!isValid}>
        <FormLabel htmlFor={label}>{label}</FormLabel>
        <Input {...useInput(label, ["isRequired"])} />
        <FormHelperText>We'll never share your email.</FormHelperText>
        <FormErrorMessage>{errors[label]}</FormErrorMessage>
      </FormControl>
    </Box>
  );
};

NOTE: I also tried to import it dynamically as shown here

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions