Re-Reren太多渲染

问题描述 投票:0回答:1

我正在关注serverless-stack.com教程。但是创建登录按钮后,我陷入了困境。

我不断收到错误:

Uncaught Error: Too many re-renders. React limits the number of renders to prevent an infinite loop.

我不知道是什么导致渲染如此多次。我合并了LoaderButton而不是将其导入使其更简单。

import React, { useState } from "react";
import { Auth } from "aws-amplify";
import { useHistory } from "react-router-dom";
import { FormGroup, FormControl, ControlLabel } from "react-bootstrap";
import { useFormFields } from "../libs/hooksLib";
import { onError } from "../libs/errorLib";
import "../css/index.css";

const LoaderButton = (
  isLoading,
  className = "",
  disabled = false,
  ...props ) => {
    return(
    <Button
      className={`LoaderButton ${className}`}
      disabled={disabled || isLoading}
      {...props}
    >
      {isLoading && <Glyphicon glyph="refresh" className="spinning" />}
      {props.children}
    </Button>
    )
};

export default function Login() {
  let history = useHistory();
  const [isLoading, setIsLoading] = useState(false);
  const [fields, handleFieldChange] = useFormFields({
    email: "",
    password: ""
  });

  function validateForm() {
    return fields.email.length > 0 && fields.password.length > 0;
  }

  async function handleSubmit(event) {
    event.preventDefault();

    setIsLoading(true);

    try {
      await Auth.signIn(fields.email, fields.password);
      userHasAuthenticated(true);
      console.log(history);
      //history.push("/");
    } catch (e) {
      onError(e);
      setIsLoading(false);
    }
  }

  return (
    <div className="Login">
      <form onSubmit={ () => { handleSubmit() } }>
        <FormGroup controlId="email" bsSize="large">
          <ControlLabel>Email</ControlLabel>
          <FormControl
            autoFocus
            type="email"
            value={fields.email}
            onChange={ () => { handleFieldChange() } }
          />
        </FormGroup>
        <FormGroup controlId="password" bsSize="large">
          <ControlLabel>Password</ControlLabel>
          <FormControl
            type="password"
            value={fields.password}
            onChange={ () => { handleFieldChange() } }
          />
        </FormGroup>
          <LoaderButton
            block
            type="submit"
            bsSize="large"
            isLoading={ () => { isLoading() } }
            disabled={() => { !validateForm() }}
          >
            Login
          </LoaderButton>
      </form>
    </div>
  );
}

hooksLib.js / useFormFields

import { useState } from 'react'

const useFormFields = (initalState) => {
  const [fields, setValues] = useState(initalState)

  return [
    fields,
    setValues({
      ...fields,
      [event.target.id]: event.target.value
    })
  ]
}

export { useFormFields }
javascript reactjs react-hooks
1个回答
0
投票

如果您想接受event值,您的自定义钩子应该看起来像这样:

const useFormFields = (initalState) => {
  const [fields, setValues] = useState(initalState)

  return [
    fields,
    (event) => setValues({
      ...fields,
      [event.target.id]: event.target.value
    })
  ]
}
© www.soinside.com 2019 - 2024. All rights reserved.