DEV Community

katongole Isaac
katongole Isaac

Posted on

React reusable components

Image description

When creating components, its better to fully optimize reactjs library for its use ( re-usability ).

Here are some key concepts to achieve resuability

  • Generalize your components

    Create components that are loosely coupled from the concept you are trying to achieve. for example when creating a form, its better to create one input component that serves many purposes. i.e

const Input = ({  name, value , onChange, ...rest } ) => {
    return <input 
        name={name}
        onChange={ e => onChange(e)}
        value={value}
        {...rest}
    />
}
Enter fullscreen mode Exit fullscreen mode
  • Raise events and don't pass handlers directly.

    Its better for a component to determine how it deals its data given either via props or state. To maintain and achieve re-usability, its better to avoid passing down handlers but raise events.

With this line of code, onChange= {e => onChange(e)} the consumer of the Input Component can pass whatever kind of handler he/she wants therefore giving you more freedom on your code.

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay