javascript React钩子呈现更多钩子问题

xxe27gdn  于 2023-01-29  发布在  Java
关注(0)|答案(1)|浏览(130)

我同时使用了两个以上的react钩子,我遇到了这个问题,除了render钩子之外,我的前端没有任何错误。我不知道如何解决这个错误。我甚至尝试使用useState方法。
如果有可能的解决办法,你能让我知道吗?

const Login = () => {
  const { register, handleSubmit, setValue } = useForm();
  const onSubmit = useCallback((formData) => {
    const { email, password } = formData;
    console.log(formData);
  }, []);
  const windowWidth = Dimensions.get("window").width;
  const windowHeight = Dimensions.get("window").height;
  const [loaded] = useFonts({
    Lato: require("../assets/fonts/Lato-Regular.ttf"),
  });

  if (!loaded) {
    return null;
  }

  const onChangeField = useCallback(
    (name) => (text) => {
      setValue(name, text);
    },
    []
  );

  useEffect(() => {
    register("email");
    register("password");
  }, [register]);
  return (
    
  );
};

export default Login;
lyr7nygr

lyr7nygr1#

你的应用每次渲染都必须调用相同数量的钩子。

if (!loaded) {
    return null;
  }

在你所有的钩子下面

const Login = () => {
  const { register, handleSubmit, setValue } = useForm();
  const onSubmit = useCallback((formData) => {
    const { email, password } = formData;
    console.log(formData);
  }, []);
  const windowWidth = Dimensions.get("window").width;
  const windowHeight = Dimensions.get("window").height;
  const [loaded] = useFonts({
    Lato: require("../assets/fonts/Lato-Regular.ttf"),
  });
  const onChangeField = useCallback(
    (name) => (text) => {
      setValue(name, text);
    },
    []
  );

  useEffect(() => {
    register("email");
    register("password");
  }, [register]);

  
  if (!loaded) {
    return null;
  }

  return (
    
  );
};

export default Login;

相关问题