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 :: _.isEqual Underscore Example 
Javascript :: invalid json text mysql 
Javascript :: javascript Detect Cycle in a Directed Graph 
Javascript :: check first path of url js 
Javascript :: onPlay 
Javascript :: ticket 
Javascript :: regex to allow spaces and characters 
Javascript :: stuck at "resvoling packages" 
Javascript :: map function usage in frontend 
Javascript :: react native scan network 
Javascript :: react random string 
Javascript :: convert javascript to java 
Javascript :: weakset use cases 
Javascript :: math min js 
Javascript :: nextjs on route change content not changing 
Javascript :: async data nuxt multiple requests 
Javascript :: use prism to render markdown in next js with gray-matter 
Javascript :: prime number in javascript using for loop 
Javascript :: regex for erlang online 
Javascript :: how to fetch devto api 
Javascript :: express roteamento 
Javascript :: create useTransaction 
Javascript :: object wrappers in javascript 
Javascript :: utm to lat long 
Javascript :: createfileinput javascript 
Javascript :: RangePicker 
Javascript :: document.querySelectorAll(".preview") + forEach 
Javascript :: AngularJS SPA edit button function 
Javascript :: Se Chartjs horizontal 
Javascript :: Use of typescript generics for extended parametres 
ADD CONTENT
Topic
Content
Source link
Name
7+7 =