Git Product home page Git Product logo

Comments (2)

juxtapos avatar juxtapos commented on May 21, 2024

I think you're getting confused as the examples on the JSX Spread page contradict the anti-pattern. Spreading to HTML elements is actually a very handy feature for being able to pass through props as HTML attributes which, from my point of view, you don't wan't to add to your component properties. A good example is a TextField component: you want it to have a specific set of component defined attributes (type="text" autocomplete="off" ...), but also support all other HTML attributes. For this to work, spread first in the HTML element, then add yours to the right, which will override props before.

The actual anti-pattern is to not spread props passed into a component which don't belong to HTML. To handle such cases, I usually use a function "omitOwnProps(this)" and spread the return value. The function reads the static defaultProps and creates an object that takes the supplied props and omits all properties from defaultProps (needs a different form for stateless components).

from react-bits.

leonardoanalista avatar leonardoanalista commented on May 21, 2024

Thanks for the explanation. I was surely confused because spread props on components is the heart of react. Second scenario, spread props on HTML can be handy but it is up to us to ensure properties are valid.

from react-bits.

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.