🚀 DAY 5 OF LAUNCH WEEK:Introducing Webhook Events for Alert Changes.Learn more →
Socket
Book a DemoInstallSign in
Socket

react-div-with-class

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-div-with-class

A React HOC to help you write cleaner code.

latest
Source
npmnpm
Version
1.1.0
Version published
Maintainers
1
Created
Source

About

react-div-with-class is a Higher-Order Component* that returns a div element with whatever class you'd like. It can be used to aid in crafting much more readable JSX.

By Example

Here's some JSX for a sidebar component:

<div className="sidebar">
  <div className="sidebar-item">
    <div className="sidebar-item-title">
      Something
    </div>
    ...
  </div>  
  <div className="sidebar-item">
    <div className="sidebar-item-title">
      Something Else
    </div>
      ...
    </div>
</div>

While this code is technically fine, something like this would be much more readable:

<Sidebar>
  <Item>
    <Title>Something</Title>
    ...
  </Item>
  <Item>
    <Title>Something Else</Title>
    ...
  </Item>
</Sidebar>

Of course, we could accomplish this by manually creating very simple React components named Sidebar,Item and Title.

That process is made trivial & speedy by using react-div-with-class. We can generate those components with the following code:

import divWithClass from 'react-div-with-class'

const Sidebar = divWithClass('sidebar')
const Item = divWithClass('sidebar-item')
const Title = divWithClass('sidebar-item-title')

react-div-with-class works like you'd hope it would. Any props passed to components it generates are passed through to the underlying div elements.

Class names are concatenated as you'd expect. So, following our previous example, the component:

<Sidebar className="sidebar-collapsed" />

would be a div with the className sidebar sidebar-collapsed.

Etc.

The code behind divWithClass() is just ~10 lines long - the primary purpose of this repo is to demonstrate how aliasing div elements can result in much easier to read markup.

That being said - feel free to use it in your projects. I use it in mine and has only a peer dependency of React.

Tests are done with Jest via npm test.

––

* I don't know if this technically qualifies as a HOC, since, while it returns a component, it doesn't take one.

Keywords

react

FAQs

Package last updated on 31 Jul 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts