GithubHelp home page GithubHelp logo

Comments (5)

nmain avatar nmain commented on April 26, 2024 1

@sylsaint React's reuse of the DOM node here is expected given the reconciliation algorithm's specifications and constraints, and the way you've structured your JSX. You're returning the same VDOM structure each time with a <button> in the same place, but only the attributes on it are different.

The docs go into a lot of depth on this sort of scenario https://react.dev/learn/preserving-and-resetting-state

from react.

nmain avatar nmain commented on April 26, 2024

The corresponding no-framework code:

<form><button type="button">Click me</button></form>
document.querySelector("button").addEventListener("click", e => { e.target.setAttribute("type", "submit"); });

will do the exact same thing and also fire the submit event. So isn't this the intended behavior? If it's not what you want, your proposed workaround with key is probably the cleanest.

from react.

gaearon avatar gaearon commented on April 26, 2024

Hm yea this seems confusing but matches how the DOM works. I think I'll close this.

from react.

sylsaint avatar sylsaint commented on April 26, 2024

@nmain Your code is based on the the same DOM node, it's ok. But in react they are actually different node, during render process, react reuse last DOM node, just modify its properties.

from react.

sylsaint avatar sylsaint commented on April 26, 2024

@gaearon Yes, it's confusing. But actually it's caused by react that reused the same DOM node for different buttons.

from react.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.