最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin4浏览0评论

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;
发布评论

评论列表(0)

  1. 暂无评论