Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

react form

import { useState } from "react"
import { createProfile } from "../endpoints/endpoints";

export default function CreateProfile() {
    const [user, setUser] = useState({})

    const handleChange = (e) => {
        setUser({...user, [e.target.name]: e.target.value});
    }

    const handleSubmit = (e) => {
        e.preventDefault();
        createProfile(user)
            .then(res => {
                window.location.href="/login";
            })
            .catch(e => console.log(e))
    }
    return (
        <>
            <h1>Create Profile</h1>
            <form>
                <label>Enter name:</label>
                <input type="text" name="name" onChange={handleChange} />
                <br/>
                <label>Enter role:</label>
                <input type="text" name="role" onChange={handleChange} />
                <br/>
                <label>Enter username:</label>
                <input type="text" name="username" onChange={handleChange} />
                <br/>
                <label>Enter password:</label>
                <input type="password" name="password" onChange={handleChange} />
                <br/>
                <button onClick={handleSubmit}>Create</button>
            </form>
        </>
    )
}
Comment

React Form

import React from "react"

export default function Form() {
    const [formData, setFormData] = React.useState(
        {
            firstName: "", 
            lastName: "", 
            email: "", 
            comments: "", 
            isFriendly: true,
            employment: "",
            favColor: ""
        }
    )
    
    function handleChange(event) {
        const {name, value, type, checked} = event.target
        setFormData(prevFormData => {
            return {
                ...prevFormData,
                [name]: type === "checkbox" ? checked : value
            }
        })
    }
    
    function handleSubmit(event) {
        event.preventDefault()
        // submitToApi(formData)
        console.log(formData)
    }
    
    return (
        <form onSubmit={handleSubmit}>
            <input
                type="text"
                placeholder="First Name"
                onChange={handleChange}
                name="firstName"
                value={formData.firstName}
            />
            <input
                type="text"
                placeholder="Last Name"
                onChange={handleChange}
                name="lastName"
                value={formData.lastName}
            />
            <input
                type="email"
                placeholder="Email"
                onChange={handleChange}
                name="email"
                value={formData.email}
            />
            <textarea 
                value={formData.comments}
                placeholder="Comments"
                onChange={handleChange}
                name="comments"
            />
            <input 
                type="checkbox" 
                id="isFriendly" 
                checked={formData.isFriendly}
                onChange={handleChange}
                name="isFriendly"
            />
            <label htmlFor="isFriendly">Are you friendly?</label>
            <br />
            <br />
            
            <fieldset>
                <legend>Current employment status</legend>
                <input 
                    type="radio"
                    id="unemployed"
                    name="employment"
                    value="unemployed"
                    checked={formData.employment === "unemployed"}
                    onChange={handleChange}
                />
                <label htmlFor="unemployed">Unemployed</label>
                <br />
                
                <input 
                    type="radio"
                    id="part-time"
                    name="employment"
                    value="part-time"
                    checked={formData.employment === "part-time"}
                    onChange={handleChange}
                />
                <label htmlFor="part-time">Part-time</label>
                <br />
                
                <input 
                    type="radio"
                    id="full-time"
                    name="employment"
                    value="full-time"
                    checked={formData.employment === "full-time"}
                    onChange={handleChange}
                />
                <label htmlFor="full-time">Full-time</label>
                <br />
            </fieldset>
            <br />
            
            <label htmlFor="favColor">What is your favorite color?</label>
            <br />
            <select 
                id="favColor" 
                value={formData.favColor}
                onChange={handleChange}
                name="favColor"
            >
                <option value="red">Red</option>
                <option value="orange">Orange</option>
                <option value="yellow">Yellow</option>
                <option value="green">Green</option>
                <option value="blue">Blue</option>
                <option value="indigo">Indigo</option>
                <option value="violet">Violet</option>
            </select>
            <br />
            <br />
            <button>Submit</button>
        </form>
    )
}
Comment

react form

const COLORS = ['white', 'red', 'blue', 'black', 'cream'];
function RegisterYourCatForm() {
  return (
    <form>
      <h2>Register Your Cat</h2>
      <label>Name*:</label>
      <input />
      <label>Color*:</label>
      <select>
        <option value="">Select color</option>
        {COLORS.map(c => <option key={c}>{c}</option>)}
      </select>
      <label>Age*:</label>
      <input />
      <label>Habits:</label>
      <textarea />
      <button type="submit">Submit</button>
    </form>
  );
}
Comment

form in react

  const [inputs, setInputs] = useState({});

  const handleChange = (event) => {
    const name = event.target.name;
    const value = event.target.value;
    setInputs(values => ({...values, [name]: value}))
  }
Comment

form react

<form>
  <label>
    Nome:
    <input type="text" name="name" />
  </label>
  <input type="submit" value="Enviar" />
</form>
Comment

react forms


https://dmitripavlutin.com/react-forms-tutorial/
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript array move element 
Javascript :: count json objects 
Javascript :: how to remove character from string in javascript 
Javascript :: jest : Cannot use import statement outside a module 
Javascript :: remove char from string js 
Javascript :: node red http post request data 
Javascript :: javascript dir 
Javascript :: smooth scroll safari 
Javascript :: format number with commas js 
Javascript :: smooth scroll mouse wheel javascript 
Javascript :: create a solid.js project 
Javascript :: sweet alert in java Script 
Javascript :: font google expo 
Javascript :: filter out undefined from object javascript 
Javascript :: javascript kill ajax request 
Javascript :: express middleware 
Javascript :: adding attribute in jquery 
Javascript :: node js sqlite3 
Javascript :: search functionality in jquery 
Javascript :: js check if div have another div 
Javascript :: disable button in jsx 
Javascript :: click events javascript 
Javascript :: json dummy data 
Javascript :: react join array of components 
Javascript :: substring 
Javascript :: javascript after 2 months date find 
Javascript :: jquery set textbox value 
Javascript :: async constructor javascript 
Javascript :: remove repeated characters from a string in javascript 
Javascript :: js if array is 2d 
ADD CONTENT
Topic
Content
Source link
Name
4+4 =