0

I appreciate this may be basic as I'm new to react, but I'm trying to just very simply log the value of the input whenever it is changed or submitted.

Here is the code:

import React from "react";

const App = () => {
  const onNameChange = (nameInput) => {
    console.log("name change Fn", nameInput);
  };

  return (
    <>
    <form onSubmit={onNameChange(this.value)}>
      <input value='the value' type="text" onChange={console.log(this.value)}></input>
    </form>
    </>
  );
}

export default App
  1. Why can I not click inside the form and change the value?
  2. How can I log the value of the input whenever clicking inside and changing it?
  3. How can I log the value of the onput on submit?

Thanks... Here is a stackblitz demo: https://stackblitz.com/edit/react-tvk69a

user8758206
  • 2,106
  • 4
  • 22
  • 45
  • it will work if you incorporate the use of `useState` & `useEffect` hooks – lala Nov 09 '20 at 15:14
  • 3
    There's several issues here. I'd recommend that you read the React documentation about controlled inputs [here](https://reactjs.org/docs/forms.html). – Brian Thompson Nov 09 '20 at 15:16
  • You are using `functional components` which in general have no `this`. You should look at `this` in vanilla JavaScript. Value of the input is available with: `onChange={(e) => console.log(e.target.value)}`. You probably need to study a bit more vanilla JavaScript. – Nano Adam Nov 09 '20 at 15:19
  • Does this answer your question? [What are React controlled components and uncontrolled components?](https://stackoverflow.com/questions/42522515/what-are-react-controlled-components-and-uncontrolled-components) – devserkan Nov 09 '20 at 15:21
  • I agree with the comments above. You should go through the documentation. Here is a forked demo which fixes the issues and uses the proper ways: https://stackblitz.com/edit/react-ddc7pi – devserkan Nov 09 '20 at 15:23

2 Answers2

2

EDITED: Why can I not click inside the form and change the value?

You are passing a hard coded value in your form Input, you need to replace it with a value you can control in your components. So you can add add something to your state to track it such as:

const [value, setValue] = React.useState('')

and interpolate it in your input element:

<input value={value}...

finally you need to handle the changes so that the state is updated:

<input value={value} onChange={e => setValue(e.target.value)}

How can I log the value of the input whenever clicking inside and changing it?

You can use the onClick and onChange props and pass there your handler:

<button onClick={activateLasers}> Activate Lasers </button>.

<input value={value} onChange={e => setValue(e.target.value)} />

How can I log the value of the onput on submit?

on the tag you can pass an onSubmit prop with your handler that will trigge from its inner type='submit'

<form onSubmit={handleSubmit}>
Mario Perez
  • 2,777
  • 1
  • 12
  • 21
2

Here U can see a full example:

import React, { useState, useEffect } from "react";

const App = () => {
  const [textV, setTextV] = useState("the value");

  const onNameChange = () => {
    event.preventDefault();
    console.log("name change Fn", textV);
  };

  useEffect(() => {
    console.log("Text has changed: ", textV);
  }, [textV]);

  return (
    <form onSubmit={onNameChange}>
      <input
        value={textV}
        type="text"
        onChange={e => setTextV(e.target.value)}
      />
      <button type="submit">Submit</button>
    </form>
  );
};

export default App;

You can play with it here: https://stackblitz.com/edit/react-ky2cks?file=src/App.js

Explanation: To take control over change of the value of text-area you should use a react state

import React, { useState, useEffect } from "react";
    
    const App = () => {
      const [textV, setTextV] = useState("the value");

Then you need to have a submit button to call the onSubmit event on the form

       <form onSubmit={onNameChange}>
          <input
            value={textV}
            type="text"
            onChange={e => setTextV(e.target.value)}
          />
          <button type="submit">Submit</button>
        </form>

In the form you can see I change the input value from "the value" to {textV} because textV is the state I declare before and in the onChange function I call the state handler function to change the value of the state textV, this function allows me to update the state each time the input value change.

To log the value of the state each time the input value change you can use the useEffect hook:

 useEffect(() => {
    console.log("Text has changed: ", textV);
  }, [textV]);
Yoandry Collazo
  • 1,122
  • 9
  • 16
  • 1
    Providing an answer is great! When providing an answer I believe some explanation would be better, also making some improvements and again explaining these to the OP is the way I like. For example, OP doesn't need to pass `textV` to the `onNameChange` function in `onSubmit` because it is already in the scope. The reference can be used there instead. Also, adding an `event.preventDefault()` would be nice for the submit handler. – devserkan Nov 09 '20 at 15:33
  • 2
    Thanks, @devserkan for your comment I tried to improve my answer following your tips. – Yoandry Collazo Nov 09 '20 at 16:03