javascript - Why is initial value not set in Material UI Autocomplete using react-hook-form? - Stack Overflow

I am using react-hook-form() in my demo.I am not able to set the default value of AutoComplete. I alr

I am using react-hook-form(/ ) in my demo.

I am not able to set the default value of AutoComplete. I already tried defaultValue as mention in the documentation but it is not working. Here is my code:

 const { register, handleSubmit, setValue } = useForm({
    defaultValues: {
      resolutionCode: 1993
    }
  });

expected output Schindler's List value should be selected

I am using react-hook-form(https://react-hook-form./ ) in my demo.

I am not able to set the default value of AutoComplete. I already tried defaultValue as mention in the documentation but it is not working. Here is my code:

https://codesandbox.io/s/react-hook-form-get-started-v24jk

 const { register, handleSubmit, setValue } = useForm({
    defaultValues: {
      resolutionCode: 1993
    }
  });

expected output Schindler's List value should be selected

Share Improve this question edited Feb 7, 2020 at 4:48 wentjun 42.6k10 gold badges107 silver badges114 bronze badges asked Feb 7, 2020 at 4:25 user944513user944513 12.8k52 gold badges185 silver badges348 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

First, you will need to use the getValues() method from react-hook-form to get the values of the form state.

const { register, handleSubmit, setValue, getValues } = useForm({
  defaultValues: {
    resolutionCode: 1993
  }
});

Then your Autoplete, you should set the defaultValue props such that it returns the object in the top100Films array that matches the year (1993).

defaultValue={top100Films.find((film) => film.year === getValues().resolutionCode)}

Here is the full changes to your code (forked your demo with the changes over here):

import React from "react";
import ReactDOM from "react-dom";
import { useForm } from "react-hook-form";
import { Button, TextField, Typography } from "@material-ui/core";
import Autoplete from "@material-ui/lab/Autoplete";

const top100Films = [
  { title: "The Shawshank Redemption", year: 1994 },
  { title: "The Godfather", year: 1972 },
  { title: "The Godfather: Part II", year: 1974 },
  { title: "The Dark Knight", year: 2008 },
  { title: "12 Angry Men", year: 1957 },
  { title: "Schindler's List", year: 1993 }
];
function App() {
  const { register, handleSubmit, setValue, getValues } = useForm({
    defaultValues: {
      resolutionCode: 1993
    }
  });

  React.useEffect(() => {
    register({ name: "resolutionCode" });
  });
  const onSubmit = data => {
    console.log(data);
  };
  return (
    <form onSubmit={handleSubmit(onSubmit)}>
      <Autoplete
        options={top100Films}
        getOptionLabel={option => option.title}
        defaultValue={top100Films.find((film) => film.year === getValues().resolutionCode)}
        onChange={(e, data) => {
          setValue("resolutionCode", data.year);
        }}
        renderInput={params => {
          return (
            <TextField
              {...params}
              // inputRef={register}
              label={"Resolution Code"}
              variant="outlined"
              name={"resolutionCode"}
              defaultValue={"1993"}
              fullWidth
              value={params}
            />
          );
        }}
      />
      <div className="button-group">
        <Button
          variant="contained"
          type="submit"
          className="MuiButton-sizeMedium"
          color="primary"
          disableElevation
        >
          Login
        </Button>
      </div>
    </form>
  );
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745234127a4617809.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信