javascript - Cannot read property 'value' of null in ReactJS - Stack Overflow

I've been trying to console.log these 2 inputs, but can't seem to figure it out, can anyone t

I've been trying to console.log these 2 inputs, but can't seem to figure it out, can anyone tell me what I've done wrong? I keep getting the Cannot read property 'value' of null error

function printInputs() {
  let username = document.getElementById('user').value
  let password = document.getElementById('pass').value

  console.log(username);
  console.log(password);

}

function App() {

  return (
    <div className="App">
      <h1>Log In</h1>
      <h1>{code}</h1>
      <form>
        <input className='userInput' id='user' type='text' placeholder='username' /><br />
        <input className='userInput' id='pass' type='password' placeholder='password' /><br />
        <input className='userSubmit' type='submit' value='Log In' onSubmit={printInputs()} />
      </form>
    </div>
  );
}

I've been trying to console.log these 2 inputs, but can't seem to figure it out, can anyone tell me what I've done wrong? I keep getting the Cannot read property 'value' of null error

function printInputs() {
  let username = document.getElementById('user').value
  let password = document.getElementById('pass').value

  console.log(username);
  console.log(password);

}

function App() {

  return (
    <div className="App">
      <h1>Log In</h1>
      <h1>{code}</h1>
      <form>
        <input className='userInput' id='user' type='text' placeholder='username' /><br />
        <input className='userInput' id='pass' type='password' placeholder='password' /><br />
        <input className='userSubmit' type='submit' value='Log In' onSubmit={printInputs()} />
      </form>
    </div>
  );
}
Share Improve this question asked Jan 14, 2020 at 14:40 AWEAWE 744 silver badges14 bronze badges 3
  • 1 You can't use document.getElementById in ReactJS stackoverflow./questions/38093760/… – ButchMonkey Commented Jan 14, 2020 at 14:42
  • Does this answer your question? How to access a DOM element in React? What is the equilvalent of document.getElementById() in React – Murat Karagöz Commented Jan 14, 2020 at 14:44
  • You have to use createRef – wentjun Commented Jan 14, 2020 at 14:44
Add a ment  | 

3 Answers 3

Reset to default 3
onSubmit={printInputs()}

You are trying to call printInputs immediately (before the render function has returned anything so before the inputs are in the page).

You need to pass a function to onSubmit:

onSubmit={printInputs}

That said, this is not the approach to take for getting data from forms in React. See the Forms section of the React guide for the right approach.

The way to write forms in react. Working example demo

function App() {
  const [state, setState] = React.useState({ username: "", password: "" });
  const handleSubmit = e => {
    e.preventDefault();
    console.log(state);
  };
  const handleChange = e => {
    setState({
      ...state,
      [e.target.name]: e.target.value
    });
  };
  return (
    <div className="App">
      <h1>Log In</h1>
      <form onSubmit={handleSubmit}>
        <input
          className="userInput"
          name="username"
          type="text"
          placeholder="username"
          onChange={handleChange}
        />
        <br />
        <input
          className="userInput"
          name="password"
          type="password"
          placeholder="password"
          onChange={handleChange}
        />
        <br />
        <input className="userSubmit" type="submit" value="Log In" />
      </form>
    </div>
  );
}

in the first never use real dom to manipulate the dom in React ,use a state to get de value and the onSumbit is used in Form tag

import React, { useState } from "React";
const App = () => {
    const [userName, setUserName] = useState("");
    const [password, setPassword] = useState("");
    const printInputs = () => {
        console.log(userName);
        console.log(password);
    };
    return (
        <div className="App">
            <h1>Log In</h1>

            <form onSubmit={printInputs}>
                <input
                    className="userInput"
                    id="user"
                    type="text"
                    placeholder="username"
                    onChange={event => setUserName(event.target.value)}
                />
                <br />
                <input
                    className="userInput"
                    id="pass"
                    type="password"
                    placeholder="password"
                    onChange={event => setPassword(event.target.value)}
                />
                <br />
                <input
                    className="userSubmit"
                    type="submit"


            value="Log In"/>
                </form>
            </div>
        );
    };
    export default App;

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744329000a4568795.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信