1

I am new to React and web development and have a question about Formik. My webpage loads correctly when I comment this part out (the values):

 <Form noValidate onSubmit={handleSubmit}>
            <Form.Row>
              <Form.Group as={Col} md="12" controlId="name">
                <Form.Label>Term</Form.Label>
                <Form.Control
                  type="text"
                  name="term"
                  placeholder="Term"
                  //value={values.term || ""}
                  onChange={handleChange}
                  isInvalid={touched.term && errors.term}
                />
                <Form.Control.Feedback type="invalid">
                  {errors.term}
                </Form.Control.Feedback>
              </Form.Group>
              <Form.Group as={Col} md="12" controlId="url">
                <Form.Label>Location</Form.Label>
                <Form.Control
                  type="text"
                  name="location"
                  placeholder="Location"
                  //value={values.location || ""}
                  onChange={handleChange}
                  isInvalid={touched.location && errors.location}
                />

However, if I enter anything into the fields, it crashes and gives me an error about Formik. If I include value, it doesn't load the page and gives me an error: 'TypeError: Cannot read property 'term' of undefined.' I am not sure what I'm doing wrong. Thanks in advance!

Edit: Here is my complete code for this file.

  return (
    <div className="home-page">
      <h1 className="center">Business Search</h1>
      <Formik validationSchema={schema} onSubmit={handleSubmit}>
        {({
          handleSubmit,
          handleChange,
          handleBlur,
          values,
          touched,
          isInvalid,
          errors,
        }) => (
          <Form noValidate onSubmit={handleSubmit}>
            <Form.Row>
              <Form.Group as={Col} md="12" controlId="name">
                <Form.Label>Term</Form.Label>
                <Form.Control
                  type="text"
                  name="term"
                  placeholder="Term"
                  value={values.term || ""}
                  onChange={handleChange}
                  isInvalid={touched.term && errors.term}
                />
                <Form.Control.Feedback type="invalid">
                  {errors.term}
                </Form.Control.Feedback>
              </Form.Group>
              <Form.Group as={Col} md="12" controlId="url">
                <Form.Label>Location</Form.Label>
                <Form.Control
                  type="text"
                  name="location"
                  placeholder="Location"
                  value={values.location || ""}
                  onChange={handleChange}
                  isInvalid={touched.location && errors.location}
                />
                <Form.Control.Feedback type="invalid">
                  {errors.location}
                </Form.Control.Feedback>
              </Form.Group>
            </Form.Row>
            <Button type="submit">Search</Button>
          </Form>
        )}
      </Formik>
      <br />
      {results.map((r, i) => {
        return (
          <Card key={i}>
            <Card.Title className="card-title">{r.name}</Card.Title>
            <Card.Body>
              <p>
                Address: {r.location.address1}, {r.location.city},{" "}
                {r.location.country}, {r.location.state}
              </p>
              <p>Rating: {r.rating}</p>
              <p>Open Now: {r.hours[0].is_open_now ? "Yes" : "No"}</p>
              <p>Hours:</p>
              <ul>
                {r.hours[0] &&
                  r.hours[0].open.map((h, i) => (
                    <li key={i}>
                      {days[h.day]}: {h.start} - {h.end}
                    </li>
                  ))}
              </ul>
            </Card.Body>
          </Card>
        );
      })}
      <br />
      <Pagination>
        <Pagination.First onClick={changePage.bind(this, 1)} />
        <Pagination.Prev
          onClick={changePage.bind(this, page - 1)}
          disabled={page === 0}
        />
        <Pagination.Next
          onClick={changePage.bind(this, page + 1)}
          disabled={Math.ceil(total / 20) === page}
        />
        <Pagination.Last
          onClick={changePage.bind(this, Math.max(Math.floor(total / 20)), 0)}
        />
      </Pagination>
    </div>
  );
}
export default HomePage;

0 Answers0