简体   繁体   English

获取未定义而不是输入字段值

[英]Getting undefined instead of Input Field value

import { useState } from "react";
import "./styles.css";

function App() {
  const [InputVal, setInputVal] = useState();
  const [ShowDiv, setShowDiv] = useState(
    <div>
      <p>Hello world. This is default "Show Div" value</p>
    </div>
  );

  const ChangeDivFunc = () => {
    setShowDiv(
      <div>
        <p style={{ color: "red" }}>
          Hello World. This is updated "Show Div" value
        </p>
        <input onChange={getInputval} type="text" />
        <br />
        <br />
        <button onClick={AlertVal}>Show Input Value</button>
      </div>
    );
  };

  const getInputval = (val) => {
    setInputVal(val.target.value);
  };

  const AlertVal = () => {
    alert(InputVal);
  };

  return (
    <div className="App">
      <h1>Example</h1>
      <br />
      <button onClick={ChangeDivFunc}>Change Div</button>
      {ShowDiv}
    </div>
  );
}

export default App;

Code Flow:代码流程:

  1. Click Change Div button then it will show Input field and Show Input Value button.单击更改 Div按钮,然后它将显示输入字段显示输入值按钮。
  2. Now, Enter some value in input field and click the Show Input Value button to get the value of input field.现在,在输入字段中输入一些值,然后单击“显示输入值”按钮以获取输入字段的值。

Problem: It returns undefined instead of input field value.问题:它返回未定义而不是输入字段值。

I'm trying to get the value of Input field when Show Input Value button is clicked.单击显示输入值按钮时,我试图获取输入字段的值。 But I'm not getting the desired results.但我没有得到预期的结果。

Here's the Sandbox link: Click for Code这是沙盒链接: 点击获取代码

Do not store html node inside state.不要将 html 节点存储在 state 内。 You can simply store only a boolean value to switch between what node to show.您可以只存储一个 boolean 值来在要显示的节点之间切换。 I'm not pretty sure but it may cause weird behavior since React internally depends heavily on the DOM/HTML UI Tree (see Managing State ).我不太确定,但它可能会导致奇怪的行为,因为 React 在内部很大程度上依赖于 DOM/HTML UI 树(请参阅 管理 State )。

Try this instead:试试这个:

import { useState } from "react";
import "./styles.css";

function App() {
  const [inputVal, setInputVal] = useState("");  // initialize as empty string.
  const [showDiv, setShowDiv] = useState(false);

  const changeDivFunc = () => {
    setShowDiv(true);
  };

  const getInputVal = (event) => {  // The arg is Event object, not val
    setInputVal(event.target.value);
  };

  const alertVal = () => {
    alert(inputVal);
  };

  return (
    <div className="App">
      <h1>Example</h1>
      <br />
      <button onClick={changeDivFunc}>Change Div</button>
      {
        showDiv? (
          <div>
            <p style={{ color: "red" }}>
               Hello World. This is updated "Show Div" value
            </p>
            <input value={inputVal} onChange={getInputVal} type="text" />
            <br />
            <br />
            <button onClick={alertVal}>Show Input Value</button>
          </div>
        ) : (
          <div>
            <p>Hello world. This is default "Show Div" value</p>
          </div>
        )
      }
    </div>
  );
}

export default App;

Forked Codepen分叉的 Codepen

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM