Answers for "form in react"

14

form in react

import React, { useState } from "react";
import "./styles.css";
function Form() {
  const [firstName, setFirstName] = useState("");
  const [lastName, setLastName] = useState("");
  const [email, setEmail] = useState("");
  const [password, setPassword] = useState("");
  return (
    <form>
      <input
        value={firstName}
        onChange={e => setFirstName(e.target.value)}
        placeholder="First name"
        type="text"
        name="firstName"
        required
      />
      <input
        value={lastName}
        onChange={e => setLastName(e.target.value)}
        placeholder="Last name"
        type="text"
        name="lastName"
        required
      />
      <input
        value={email}
        onChange={e => setEmail(e.target.value)}
        placeholder="Email address"
        type="email"
        name="email"
        required
      />
      <input
        value={password}
        onChange={e => setPassword(e.target.value)}
        placeholder="Password"
        type="password"
        name="password"
        required
      />
      <button type="submit">Submit</button>
    </form>
  );
}
export default Form;
Posted by: Guest on July-15-2020
2

form react

// perfect form
import React, { useState } from "react";
import axios from "axios";
import { toastError, toastSuccess } from "./Toast";
import { useHistory } from "react-router-dom";

export function FetchAPI() {
  const history = useHistory();

  const [user, setUser] = useState({
    firstName: "",
    lastName: "",
    email: "",
    password: "",
  });

  const onInputChange = async (e) => {
    setUser({ ...user, [e.target.name]: e.target.value });
  };
  const { firstName, lastName, email, password } = user;

  const handleSubmit = async (e) => {
    e.preventDefault();
    axios
      .post(`http://localhost:4000/user`, user)
      .then((res) => {
        console.log("~ res", res)
        toastSuccess("User successfully registered");
      })
      .catch((error) => {
        console.log(error);
        toastError("Useris not created try again");
      });
    history.push("/displayUser");
    console.log("Out side handle");
  };
  return (
    <form onSubmit={(e) => handleSubmit(e)}>
      <center>
        <div style={{ marginTop: "40px" }}>
          <label>
            First Name:
            <input
              type="text"
              value={firstName}
              name="firstName"
              onChange={(e) => onInputChange(e)}
              required
            />
          </label>
          <br />
          <label>
            last Name:
            <input
              type="text"
              value={lastName}
              name="lastName"
              onChange={(e) => onInputChange(e)}
            />
          </label>
          <br />
          <label>
            email:
            <input
              type="email"
              value={email}
              name="email"
              onChange={(e) => onInputChange(e)}
              required
            />
          </label>
          <br />
          <label>
            password:
            <input
              type="text"
              value={password}
              name="password"
              onChange={(e) => onInputChange(e)}
              required
            />
          </label>
          <br />
          <br />
          <button>Submit </button>
        </div>
      </center>
    </form>
  );
}
export default FetchAPI;
Posted by: Guest on August-27-2021
9

react text input onchange

class NameForm extends React.Component {
  constructor(props) {
    super(props);
    this.state = {value: ''};
    this.handleChange = this.handleChange.bind(this);
    this.handleSubmit = this.handleSubmit.bind(this);
  }

  handleChange(event) {    this.setState({value: event.target.value});  }
  handleSubmit(event) {
    alert('A name was submitted: ' + this.state.value);
    event.preventDefault();
  }

  render() {
    return (
      <form onSubmit={this.handleSubmit}>
        <label>
          Name:
          <input type="text" value={this.state.value} onChange={this.handleChange} />        </label>
        <input type="submit" value="Submit" />
      </form>
    );
  }
}
Posted by: Guest on May-31-2020
1

react hook form rating material ui

import React, { useState } from "react";
import Checkbox from "@material-ui/core/Checkbox";
import Button from "@material-ui/core/Button";
import TextField from "@material-ui/core/TextField";
import FormControlLabel from "@material-ui/core/FormControlLabel";
import Typography from "@material-ui/core/Typography";
import { makeStyles } from "@material-ui/core/styles";
import Container from "@material-ui/core/Container";
import { useForm } from "react-hook-form";
import Rating from "@material-ui/lab/Rating";
import StarBorderIcon from '@material-ui/icons/StarBorder';

const useStyles = makeStyles((theme) => ({
  paper: {
    marginTop: theme.spacing(8),
    display: "flex",
    flexDirection: "column",
    alignItems: "center"
  },
  form: {
    width: "100%", // Fix IE 11 issue.
    marginTop: theme.spacing(1)
  },
  submit: {
    margin: theme.spacing(3, 0, 2)
  }
}));

export default function Create() {
  const classes = useStyles();
  const [rating, setRating] = useState(2);
  const { register, handleSubmit } = useForm();
  const onSubmit = (data) => {
    console.log(data);
  };

  return (
    <Container component="main" maxWidth="xs">
      <div className={classes.paper}>
        <Typography component="h1" variant="h5">
          Form
        </Typography>
        <form
          className={classes.form}
          noValidate
          onSubmit={handleSubmit(onSubmit)}
        >
          <TextField
            variant="outlined"
            margin="normal"
            fullWidth
            id="title"
            label="Title"
            name="title"
            autoFocus
            inputRef={register()}
          />
          <FormControlLabel
            control={
              <Checkbox
                inputRef={register}
                name="remember"
                defaultValue={false}
              />
            }
            label="remember"
          />
          <br />
          <FormControlLabel
            control={
              <>
                <input
                  name="rating"
                  type="number"
                  value={rating}
                  ref={register}
                  hidden
                  readOnly
                />
                <Rating
                  name="rating"
                  value={rating}
                  precision={0.5}
                  onChange={(_, value) => {
                    setRating(value);
                  }}
                  icon={<StarBorderIcon fontSize="inherit" />}
                />
              </>
            }
            label="select rating"
          />
          <Button
            type="submit"
            fullWidth
            variant="contained"
            color="primary"
            className={classes.submit}
          >
            Submit
          </Button>
        </form>
      </div>
    </Container>
  );
}
Posted by: Guest on October-04-2020
4

html select react

class FlavorForm extends React.Component {
  constructor(props) {
    super(props);
    this.state = {value: 'coconut'};
    this.handleChange = this.handleChange.bind(this);
    this.handleSubmit = this.handleSubmit.bind(this);
  }

  handleChange(event) {    this.setState({value: event.target.value});  }
  handleSubmit(event) {
    alert('Your favorite flavor is: ' + this.state.value);
    event.preventDefault();
  }

  render() {
    return (
      <form onSubmit={this.handleSubmit}>
        <label>
          Pick your favorite flavor:
          <select value={this.state.value} onChange={this.handleChange}>            <option value="grapefruit">Grapefruit</option>
            <option value="lime">Lime</option>
            <option value="coconut">Coconut</option>
            <option value="mango">Mango</option>
          </select>
        </label>
        <input type="submit" value="Submit" />
      </form>
    );
  }
}
Posted by: Guest on May-15-2020
1

form react

<form>
  <label>
    Nome:
    <input type="text" name="name" />
  </label>
  <input type="submit" value="Enviar" />
</form>
Posted by: Guest on May-20-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language