简体   繁体   English

<form> React 中的元素不像我期望的那样工作</form>

[英]<form> element in React does not work like i expect it to

I'm trying to create a form element where the user can enter input.我正在尝试创建一个用户可以输入输入的表单元素。 If I insert the form element directly, everything works fine when typing the letters.如果我直接插入表单元素,输入字母时一切正常。 If I insert the form element via AddForm component, I can only type one letter and have to click into the field again to enter the next letter.如果我通过AddForm组件插入表单元素,我只能输入一个字母,并且必须再次单击该字段才能输入下一个字母。

Why is that?这是为什么?

import ReactDOM from 'react-dom';
import React, { useState } from 'react'

const App = () => {

    const [newName, setName] = useState("")
    const [newNumber, setNumber] = useState("")

    const handleName = (event) => {
        setName(event.target.value)
    }

    //doesn't work the way I expect it to
    const AddForm = () => {
        return (
            <form>
                name: <input value={newName} onChange={e => setName(e.target.value)} />
                <div><button type="submit">add</button></div>
            </form>

        )
    }

    return (
        <div>
            <AddForm />
            //does work
            <form>
                name: <input value={newName} onChange={handleName} />
                <div><button type="submit">add</button></div>
            </form>
        </div>
    )
}

ReactDOM.render(<App />, document.getElementById('root'));

AddForm function is created on every render so from React perspective AddForm is a different component causing the related DOM elements getting recreated which makes it look like the input element lost its focus. AddForm function 是在每次渲染时创建的,因此从React的角度来看, AddForm是一个不同的组件,导致相关的 DOM 元素被重新创建,这使得它看起来像输入元素失去了焦点。

Move AddForm outside of App (and pass required data into it through props).AddForm App之外(并通过 props 将所需的数据传递给它)。


why is AddForm not re-rendered when it is outside App and why are the elements not re-rendered when they are directly inside App?为什么 AddForm 在 App 外部时不重新渲染,为什么元素直接在 App 内部时不重新渲染?

React updates the DOM in an effective way, only changing things that are different but it's not perfect. React 以有效的方式更新 DOM,只改变了不同但并不完美的东西。 It compares components with === and if they don't match, the whole tree is removed.它将组件与===进行比较,如果它们不匹配,则删除整个树。

Since you recreate AddForm on every render, current AddFrom is not equal to previous AddFrom .由于您在每次渲染时重新创建AddForm ,因此当前AddFrom不等于先前AddFrom This makes React remove the form associated with AddForm and create another one in its place.这使得 React 删除了与AddForm关联的form并在其位置创建另一个表单。

When you put AddForm outside, current AddForm is equal to previous AddForm so React just compares the return values of previous and current AddForm and updates the DOM associated with AddForm (that is, just adds another char to the input field)当你把AddForm放在外面时,当前的AddForm等于先前的AddForm所以 React 只是比较先前和当前的AddForm的返回值并更新与AddForm关联的 DOM(即只是在输入字段中添加另一个字符)

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

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