3

Using React Hooks to Get Input Value

 2 years ago
source link: https://dev.to/tulusibrahim/using-react-hooks-to-get-input-value-4f25
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Hi! Today we will learn how to use react hooks in a simple way in order to get strong basic fundamental in react hooks.

The first thing we need to set up is of course react JS environment which you can refer to their documentation. If all is ready, then we are good to go!

First Step

Lets open up project file then open App.js file, there you can start by importing react and hooks useState so we can use it later. Here is how it looks.

import React, { useState } from 'react'
Enter fullscreen modeExit fullscreen mode

Next is creating function called App, here we using functional component so it is aligned because we are going to use react hooks.

function App(){

}
export default App;
Enter fullscreen modeExit fullscreen mode

Don’t forget to import it in the end of our code so it does not produce error.

Second Step

function App(){
const [name, setName] = useState('')
const [password, setPassword] = useState('')
}
Enter fullscreen modeExit fullscreen mode

There we define name, setName. Name is the variable that we can use to display the value it has. Meanwhile setName is the setter method that we can use to change the value of name. useState is to define the initial value of the Name, it can be empty string, empty array, boolean, etc. To get better explanation of react hooks, you can refer to their docs.

Third Step

We need to provide return inside our App component so it will display something to the screen.

function App() {
  const [name, setName] = useState('');
  const [password, setPassword] = useState('');

  return (
    <div>
      <input placeholder="username" onChange={e => setName(e.target.value)} />
      <input
        placeholder="password"
        onChange={e => setPassword(e.target.value)}
      />
      <button onClick={() => handleInput()}>Input</button>
      {name ? <p>Welcome {name}!</p> : ''}
    </div>
  );
}
Enter fullscreen modeExit fullscreen mode

Looks terrible? yes. Calm down, I’ll explain it.

  • So the first thing is we create a div tag, which will wraps up all other element. There I added some style to the div tag.
  • Next is we define two input tag, a button with some placeholder in it
  • Then we define onChange props inside input tag. Inside onChange props we define anonymous function that has e parameter, so then we can get access to the value inside input tag. (The anonymous function is using arrow function, you can check it through W3School if you’re not familiar yet with it.)
  • Then inside anonymous function, we call the setter method that I already explain before, and we also pass the e parameter inside setter method, so the value of name, password is change every time the value inside input tag is changing.
const handleInput =  () =>{
    console.log(name, password)
  }
Enter fullscreen modeExit fullscreen mode
  • There I also add a button with handleInput method just to console.log it to make sure everything is works.
  • Beneath the button, I added ternary operator to show some text if the name variable is filled with something.

Here is the looks of the whole code that we write so far.

import React, { useState } from 'react';

function App() {
  const [name, setName] = useState('');
  const [password, setPassword] = useState('');

  const handleInput =  () =>{
    console.log(name, password)
  }

  return (
    <div>
      <input placeholder="username" onChange={e => setName(e.target.value)} />
      <input
        placeholder="password"
        onChange={e => setPassword(e.target.value)}
      />
      <button onClick={() => handleInput()}>Input</button>
      {name ? <p>Welcome {name}!</p> : ''}
    </div>
  );
}
export default App
Enter fullscreen modeExit fullscreen mode

Wrap’s up! Those three simple step is enough to use hooks in simple way just to get started and get solid basic understanding about react hooks. Thank you for reading up to this point, see ya!✨


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK