简体   繁体   English

为什么我使用钩子的简单 React 表单不起作用?

[英]Why does my simple React form using hooks not work?

I'm learning the basics of React Hooks at the moment, I am wondering why this particular code does not work:我现在正在学习 React Hooks 的基础知识,我想知道为什么这个特定的代码不起作用:

function Form() {
  const [input, setInput] = useState('');
  let result = '';
  function handleSubmit(e) {
    e.preventDefault();
    result = input;
  }
  return (
    <React.Fragment>
      <h1>Form practice</h1>
      <form>
        <input type="text" onChange={(e) => setInput(e.target.value)}></input>
        <button type="submit" onClick={handleSubmit}>
          submit
        </button>
      </form>
      <h2>{result}</h2>
    </React.Fragment>
  );
}

I am simply expecting <h2>{result}</h2> to display what is being captured in input while the submit button is clicked.我只是希望<h2>{result}</h2>在单击提交按钮时显示input中捕获的内容。 However, nothing happens.然而,什么也没有发生。 What is wrong with this logic?这个逻辑有什么问题?

The way that a react component knows to rerender is that you set state. react 组件知道重新渲染的方式是设置 state。 Changing the value of a local variable will not cause a rerender.更改局部变量的值不会导致重新渲染。 So if you want the <input> to change separately from the <h2> , you will need to turn result into a piece of state.因此,如果您希望<input><h2>分开更改,则需要将result转换为 state。

const [input, setInput] = useState('');
const [result, setResult] = useState('');
function handleSubmit(e) {
  e.preventDefault();
  setResult(input);
}
return (
  <React.Fragment>
    <h1>Form practice</h1>
    <form>
      <input type="text" onChange={(e) => setInput(e.target.value)}></input>
      <button type="submit" onClick={handleSubmit}>
        submit
      </button>
    </form>
    <h2>{result}</h2>
  </React.Fragment>
);

Simply because on each render of the component (whenever the state change, the component will render again) the code runs again and result will set to value 0.仅仅是因为在组件的每次渲染中(每当 state 更改时,组件将再次渲染)代码再次运行并且result将设置为值 0。

Why not just render the input instead of the result variable?为什么不只渲染input而不是result变量?

 function Form() { const [input, setInput] = useState(''); function handleSubmit(e) { // do whatever you need to do to submit the form. } return ( <React.Fragment> <h1>Form practice</h1> <form> <input type="text" onChange={(e) => setInput(e.target.value)}></input> <button type="submit" onClick={handleSubmit}> submit </button> </form> <h2>{input}</h2> </React.Fragment> ); }

Hi ye need to set the result with useState as well so that re-render happens嗨,您还需要使用 useState 设置结果,以便重新渲染

function Form() {
  const [input, setInput] = useState('');
  const [result,setResult]= useState('');
  const handleSubmit(e) {
    e.preventDefault();
    setResult(input);
  }
  return (
    <React.Fragment>
      <h1>Form practice</h1>
      <form>
        <input type="text" onChange={(e) => setInput(e.target.value)}></input>
        <button type="submit" onClick={handleSubmit}>
          submit
        </button>
      </form>
      <h2>{result}</h2>
    </React.Fragment>
  );
}

now u will be able to see the changes现在您将能够看到更改

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

相关问题 为什么我在 React 中的简单名称更改 function 不起作用? - Why does my simple name change function in React not work? 为什么映射 React 钩子有效,但 lambda 违反钩子规则? - Why does mapping a React hook work, but a lambda violates rule of hooks? 为什么我的 state 没有使用 React Hooks 更新? - Why is my state not being updated using React Hooks? 使用 React 和 Redux Hooks,为什么我的操作没有触发? - Using React and Redux Hooks, why is my action not firing? 我在菜单项上使用反应钩子。 我的第一次点击没有做任何事情,后续点击按预期工作 - I am using react hooks on menu item. My first click does not do anything, subsequent clicks work as intended React hooks 在 Gatsby 组件中不起作用 - React hooks does not work in Gatsby components 当我使用 React 钩子和 React 上下文添加新帖子时,为什么我的 state 会替换我的帖子 - Why is my state replacing my posts when I add a new one using React hooks and React context react hooks为什么会出现TypeError - Why does TypeError occur in react hooks 为什么用反应钩子过滤状态数组不起作用但过滤原始数组 - why filtering state array with react hooks doesn't work but filtering original array does 为什么当我尝试使用 React Native 钩子时,它无法按预期正常工作? - Why when I try to use react native hooks it does not work properly as intended to be?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM