handleClick 直到我点击两次后才更新我的显示值。

huangapple go评论64阅读模式
英文:

handleClick isn't updating my displayed value until I've clicked it twice

问题

I've set a handleClick function that should update the state of my variable when called, and it does, but I also need the displayed variable to update on-screen from the same click. It was working earlier, but has since stopped, no idea what I changed because I've yet to learn version control (my bad.)

This is the code in question.

import React, { useState } from 'react';
import Numbergen from './Numbergen.js';
import Comparenums from './Comparenums.js';

const Guessbox = () => {
    const [userInput, setUserInput] = useState(0);
    const [guess, setGuess] = useState(userInput);
    const [numToGuess, setNumToGuess] = useState(Numbergen());
    const [result, setResult] = useState('');

    const handleChange = (event) => {
        setUserInput(event.target.value);
    };

    const handleClick = () => {
        setGuess(userInput);
        setResult(Comparenums(guess, numToGuess));
    }

    return (
        <div>
            <input
                type="text"
                id="userInput"
                name="userInput"
                onChange={handleChange}
                value={userInput}
            />
            <button onClick={handleClick}>Guess!</button>
            <h2>{result}</h2>
            <h2>{numToGuess}</h2>
        </div>
    );
}

export default Guessbox;

I'm very new at all this, so I apologize for any faux pas in my post.

I've tried adding this. to my useState variables because I saw that worked for some other people in similar situations, kind of hoping for a hail mary on that one. I also tried calling handleClick twice in the same onClick, expecting it might call it twice to both set and update the variable onscreen, it did not do that.

英文:

I've set a handleClick function that should update the state of my variable when called, and it does, but I also need the displayed variable to update on-screen from the same click. It was working earlier, but has since stopped, no idea what I changed because I've yet to learn version control (my bad.)

This is the code in question.

import React, {useState} from &#39;react&#39;;
import Numbergen from &#39;./Numbergen.js&#39;;
import Comparenums from &#39;./Comparenums.js&#39;;



const Guessbox = () =&gt; {
    const [userInput, setUserInput] = useState(0);
    const [guess, setGuess] = useState(userInput)
    const [numToGuess, setNumToGuess] = useState(Numbergen());
    const [result, setResult] = useState(&#39;&#39;)

    const handleChange = (event) =&gt; {
        setUserInput(event.target.value);
    };

    const handleClick = () =&gt; {
        setGuess(userInput);
        setResult(Comparenums(guess,numToGuess));
    }

    return (
        &lt;div&gt;
            &lt;input
                type=&quot;text&quot;
                id=&quot;userInput&quot;
                name=&quot;userInput&quot;
                onChange={handleChange}
                value={userInput}
            /&gt;
            &lt;button onClick={handleClick}&gt;Guess!&lt;/button&gt;
            &lt;h2&gt;{result}&lt;/h2&gt;
            &lt;h2&gt;{numToGuess}&lt;/h2&gt;
        &lt;/div&gt;
    );
}


export default Guessbox;

I'm very new at all this, so I apologize for any faux pas in my post.

I've tried adding this. to my useState variables because I saw that worked for some other people in similar situations, kind of hoping for a hail marry on that one. I also tried calling handleClick twice in the same onClick, expecting it might call it twice to both set and update the variable onscreen, it did not do that.

答案1

得分: 0

const [userInput, setUserInput] = useState(0);
const [guess, setGuess] = useState(userInput); // 我认为你不需要使用这个。
const [numToGuess, setNumToGuess] = useState(Numbergen()); // 这个变量可能是 const 吗?像 numToGuess = Numbergen();
const [result, setResult] = useState('')

const handleChange = (event) => {
setUserInput(event.target.value);
};

const handleClick = () => {
// setGuess(userInput)
// setResult(Comparenums(guess,numToGuess));
setResult(Comparenums(userInput,numToGuess));
}

英文:
    const [userInput, setUserInput] = useState(0);
    const [guess, setGuess] = useState(userInput); // I think you don&#39;t need to use this.
    const [numToGuess, setNumToGuess] = useState(Numbergen()); // This variable might be const? like numToGuess = Numbergen();
    const [result, setResult] = useState(&#39;&#39;)

    const handleChange = (event) =&gt; {
        setUserInput(event.target.value);
    };

    const handleClick = () =&gt; {
        // setGuess(userInput)
        // setResult(Comparenums(guess,numToGuess));
        setResult(Comparenums(userInput,numToGuess));
    }

答案2

得分: 0

SOLUTION 1

您不必创建另一个状态。当您创建具有其初始值的另一个状态时,它需要更新。

const [guess, setGuess] = useState(userInput);

它将最初更新为 0

当您在 handleChange 中更新它时,它将在下一个时刻更新,而您正在将它与上次更新的值进行比较,这是不正确的。只需创建一个单一的状态,无需更新它,只需将其转换为数字后与该值进行比较。

Working solution

const Guessbox = () => {
  const [userInput, setUserInput] = useState(0);
  // const [guess, setGuess] = useState(userInput); // CHANGE
  const [numToGuess, setNumToGuess] = useState(Numbergen());
  const [result, setResult] = useState("");

  const handleChange = (event) => {
    let value = parseInt(event.target.value || "0", 10);
    setUserInput(value);
  };

  const handleClick = () => {
    const result = Comparenums(userInput, numToGuess); // CHANGE
    setResult(result);
  };

  return (
    <div>
      <input
        type="text"
        id="userInput"
        name="userInput"
        onChange={handleChange}
        value={userInput}
      />
      <button onClick={handleClick}>Guess!</button>
      <h2>{result}</h2>
      <h2>{numToGuess}</h2>
    </div>
  );
};

SOLUTION 2

出于任何原因,假设您希望将其与 guess 进行比较,那么当 userInput 更改时,您必须更新 guess,以便在单击 guess 按钮时,guess 状态将已更新:

const handleClick = () => {
  setResult(Comparenums(guess, numToGuess));
};

useEffect(() => {
  const number = parseInt(userInput || "0", 10);
  setGuess(number);
}, [userInput]);
英文:

SOLUTION 1

You don't have to create another state. When you crate another state with its initial value then it needs to be updated.

const [guess, setGuess] = useState(userInput);

It will initially updated with 0

and when you update it in handleChange then it will update in next tick and you are comparing it will last updated value which won't be right. Just create a single state and no need to update it just compare it with that value after convert it into number.

Working solution

const Guessbox = () =&gt; {
  const [userInput, setUserInput] = useState(0);
  // const [guess, setGuess] = useState(userInput); // CHANGE
  const [numToGuess, setNumToGuess] = useState(Numbergen());
  const [result, setResult] = useState(&quot;&quot;);

  const handleChange = (event) =&gt; {
    let value = parseInt(event.target.value || &quot;0&quot;, 10);
    setUserInput(value);
  };

  const handleClick = () =&gt; {
    const result = Comparenums(userInput, numToGuess); // CHANGE
    setResult(result);
  };

  return (
    &lt;div&gt;
      &lt;input
        type=&quot;text&quot;
        id=&quot;userInput&quot;
        name=&quot;userInput&quot;
        onChange={handleChange}
        value={userInput}
      /&gt;
      &lt;button onClick={handleClick}&gt;Guess!&lt;/button&gt;
      &lt;h2&gt;{result}&lt;/h2&gt;
      &lt;h2&gt;{numToGuess}&lt;/h2&gt;
    &lt;/div&gt;
  );
};

SOLUTION 2

For any reason let say you want to compare it with guess then you have to update guess when userInput changes, so that when you click on the guess button then on then time of comparison guess state would have updated:

  const handleClick = () =&gt; {
    setResult(Comparenums(guess, numToGuess));
  };

  useEffect(() =&gt; {
    const number = parseInt(userInput || &quot;0&quot;, 10);
    setGuess(number);
  }, [userInput]);

huangapple
  • 本文由 发表于 2023年6月15日 09:27:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76478514.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定