簡體   English   中英

從 ReactJS 中的輸入值獲取未定義的值

[英]Getting undefined values from Input Value in ReactJS

我無法從 FormControl Input值中獲取emailpassword的值。 我將BaseWeb ReactUI 框架用於字段 UI。

需要幫助解決這個問題。

import { FormControl } from 'baseui/form-control';
import { Input } from 'baseui/input';
import { useStyletron } from 'baseui';
import { Alert } from 'baseui/icon';
import { Button } from 'baseui/button';
import { TiUserAddOutline } from "react-icons/ti";
import Axios from "axios";
import { useHistory, Link } from 'react-router-dom';
import { validate as validateEmail } from 'email-validator'; // add this package to your repo with `$ yarn add email-validator`
import { Select } from 'baseui/select';
import { Checkbox } from 'baseui/checkbox';

function SelectAtStart(props) {
  const [css] = useStyletron();
  return (
    <div className={css({ display: 'flex' })}>
      <div className={css({ width: '200px', paddingRight: '8px' })}>
        <Select
          options={props.options}
          labelKey="id"
          valueKey="gender"
          onChange={({ value }) => props.onSelectChange(value)}
          value={props.selectValue}
          id={props.id}
        />
      </div>
      <Input
        onChange={e => props.onInputChange(e.target.value)}
        value={props.inputValue}
      />
    </div>
  );
}

function Negative() {
  const [css, theme] = useStyletron();
  return (
    <div
      className={css({
        display: 'flex',
        alignItems: 'center',
        paddingRight: theme.sizing.scale500,
        color: theme.colors.negative400,
      })}
    >
      <Alert size="18px" />
    </div>
  );
}

export function RegisterFields() {
  const history = useHistory();
  const [css, theme] = useStyletron();
  const [email, setEmail] = React.useState('');
  const [password, setPassword] = React.useState('');
  const [checked, setChecked] = React.useState(true);
  const [startInputValue, setStartInputValue] = React.useState('');
  const [startSelectValue, setStartSelectValue] = React.useState(
    [],
  );
  const [isValid, setIsValid] = React.useState(false);
  const [isVisited, setIsVisited] = React.useState(false);
  const shouldShowError = !isValid && isVisited;
  const onChangeEmail = ({ target: { email } }) => {
    setIsValid(validateEmail(email));
    setEmail(email);

  };
  
  const onChangePassword = ({ target: { password } }) => {
    setIsValid(true);
    setPassword(password);
  }

  
  const handleSubmit = (event) => {
    event.preventDefault();

    console.log(email, password)
    Axios.defaults.headers.common = {
      "Content-Type": "application/json"
    }

    Axios.post("http://localhost:5000/api/signup", {
      email: email,
      password: password,
      firstName: startInputValue,
      lastname: startInputValue,
      agreement: checked
    }).then((res) => {
      if (res.status === 200) {
        const path = '/dashboard'
        history.push(path)
        console.log(res)
      }
      else {
        console.log("Unable to create account")
      }
    })
  }

  return (
    <form
      onSubmit={handleSubmit}
      className={css({
        marginTop: theme.sizing.scale1000,
      })}
    >
      <FormControl
        label="Your Email"
        error={
          shouldShowError
            ? 'Please input a valid email address'
            : null
        }
      >
        <Input
          id="email"
          value={email}
          onChange={onChangeEmail}
          onBlur={() => setIsVisited(true)}
          error={shouldShowError}
          overrides={shouldShowError ? { After: Negative } : {}}
          type="email"
          required
        />

      </FormControl>
      <FormControl
        label="Your Password"
        error={
          shouldShowError
            ? 'Your password is incorrect'
            : null
        }
      >
        <Input
          id="password"
          value={password}
          onChange={onChangePassword}
          onBlur={() => setIsVisited(true)}
          error={shouldShowError}
          overrides={shouldShowError ? { After: Negative } : {}}
          type="password"
          required
        />

      </FormControl>
      <FormControl
        label="Your Full Name"
      >
        <SelectAtStart
          inputValue={startInputValue}
          onInputChange={v => setStartInputValue(v)}
          selectValue={startSelectValue}
          onSelectChange={v => setStartSelectValue(v)}
          options={[
            { id: 'Mr', gender: 'Male' },
            { id: 'Mrs', gender: 'Women' },
            { id: 'Ms', gender: 'Female' },
            { id: 'None', gender: 'Dont Say' },
          ]}
          id="start-id"
        />
      </FormControl>
      <FormControl>
        <Checkbox
          checked={checked}
          onChange={() => setChecked(!checked)}
        >
          <div className={css({
            color:'grey'
          })}
          >I hereby agree to the terms and conditons of the platform.</div>
        </Checkbox>
      </FormControl>
      <Button type="submit">
        <TiUserAddOutline style={{ marginRight: '10px' }} />
        Create Account
      </Button>
      <div>
        <p style={{ marginTop: '10px', color: 'grey' }} >To go back to login, please <Link to='/'>click here</Link></p>
      </div>
    </form>
  );
}

我相信這是因為您一直將其設置為“未定義”的目標的電子郵件/密碼值。

e.target.email = 未定義
e.target.password = 未定義

嘗試使用這種方法:

const onChangeEmail = e =>
{
    setIsValid(validateEmail(e.target.value));
    setEmail(e.target.value);
};

const onChangePassword = e =>
{
    setIsValid(true);
    setEmail(e.target.value);
};

在這里您可以看到所有文本輸入都存儲在目標事件的值中( event.target.value )

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM