Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

get input value in react using hooks

import React, {useState} from 'react'

export default () => {
    const [fName, setfName] = useState('');
    const [lName, setlName] = useState('');
    const [phone, setPhone] = useState('');
    const [email, setEmail] = useState('');

const submitValue = () => {
    const frmdetails = {
        'First Name' : fName,
        'Last Name' : lName,
        'Phone' : phone,
        'Email' : email
    }
    console.log(frmdetails);
}

return(
    <>
    <hr/>
    <input type="text" placeholder="First Name" onChange={e => setfName(e.target.value)} />
    <input type="text" placeholder="Last Name" onChange={e => setlName(e.target.value)} />
    <input type="text" placeholder="Phone" onChange={e => setPhone(e.target.value)} />
    <input type="text" placeholder="Email" onChange={e => setEmail(e.target.value)} />
    <button onClick={submitValue}>Submit</button>
    </>
    )
}
Comment

Handle an input with React hooks

  const [inputValue, setInputValue] = React.useState("");

      const onChangeHandler = event => {
        setInputValue(event.target.value);
      };

      <input
        type="text"
        name="name"
        onChange={onChangeHandler}
        value={inputValue}
      />
Comment

Which React hook we use to get value from an input box?

import {useRef} from 'react';

const App = () => {
  const inputRef = useRef(null);

  function handleClick() {
    console.log(inputRef.current.value);
  }

  return (
    <div>
      <input
        ref={inputRef}
        type="text"
        id="message"
        name="message"
      />

      <button onClick={handleClick}>Log message</button>
    </div>
  );
};

export default App;
Comment

PREVIOUS NEXT
Code Example
Javascript :: angular timestamp 
Javascript :: onclick event in angular 
Javascript :: new line in js 
Javascript :: include gif in react 
Javascript :: js check if object is empty 
Javascript :: angular adding delay 
Javascript :: ERR_REQUIRE_ESM 
Javascript :: ajax image post ekleme 
Javascript :: add commas to a number javascript 
Javascript :: js when you leave 
Javascript :: random in a range js 
Javascript :: js string contains substring ignore case 
Javascript :: javascript select all divs with class 
Javascript :: how to find the last item in an array 
Javascript :: nl2br in jquery 
Javascript :: js indexof nested array 
Javascript :: iframe player youtube onfinish event 
Javascript :: pass an array to javascript in asp net core list 
Javascript :: remove first char javascript 
Javascript :: electron Uncaught ReferenceError: require is not defined at recorder.js:1 
Javascript :: node-json-db example 
Javascript :: moment add seconds 
Javascript :: javascript to help find overflow elements 
Javascript :: jest ReferenceError: TextEncoder is not defined 
Javascript :: node js run bat file 
Javascript :: how to reverse an array in javascript 
Javascript :: `object` ("[object Object]") cannot be serialized as JSON. Please only return JSON serializable data types 
Javascript :: remove console log in production react 
Javascript :: digit count in javascript 
Javascript :: js selection box excel node 
ADD CONTENT
Topic
Content
Source link
Name
7+9 =