npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

dumb-react

v1.0.2

Published

Creates dumb React components using only a render method

Downloads

6

Readme

dumb-react

Smart and Dumb Components

Quick Start

const dumb = require('dumb-react')

const Hello = dumb(function Hello(props) {
    return (
        <div>Hello {props.name}</div>
    )
})

// optional step:
Hello.propTypes = { name: React.PropTypes.string.isRequried }

How do I handle state/context/lifecycle hooks?

This is intended for building the very simple components that don't need any of those things. You can append them manually if you like, but I'm not sure why you would.

Why would I use this?

When writing React components you often have sections of the UI that you display conditionally, or you generate from a list of data. There are a few options on how to accomplish these. Here's a sample where I chose to create variables in my render()

export default class Sample extends React.Component {
    render() {
        const { items, total } = this.props

        const itemElements = items.map((item) => {
            return (
                <li key={item.id}>
                    {item.name}
                </li>
            )
        })

        let moreButton
        if (items.length < total) {
            moreButton = (
                <button onClick={this.onLoadMore.bind(this)}>Load more</button>
            )
        }

        return (
            <div>
                Results:
                <ul>
                    {itemElements}
                </ul>
                {moreButton}
            </div>
        )
    }
}

Personally, I don't really like how render just keeps growing and is doing a bunch of different things. I have been splitting these into different render methods in the same component. I have a choice here. I could pass items and total to the functions that need them, or I could just call the functions with no parameters and let them handle pulling what they need off of this.props.

export default class Sample extends React.Component {
    renderItemElements(items) {
        return items.map((item) => {
            return (
                <li key={item.id}>
                    {item.name}
                </li>
            )
        })
    }
    renderMoreButton(items, total) {
        if (items.length < total) {
            return (
                <button onClick={this.onLoadMore.bind(this)}>Load more</button>
            )
        }
    }
    render() {
        const { items, total } = this.props
        return (
            <div>
                Results:
                <ul>
                    {this.renderItemElements(items)}
                </ul>
                {this.renderMoreButton(items, total)}
            </div>
        )
    }
}

Those render methods don't really do much. Why not make them their own components? This is what dumb-react is for

const ItemElements = dumb(function ItemElements(props) {
    return props.items.map((item) => {
        return (
            <li key={item.id}>
                {item.name}
            </li>
        )
    })
})

const MoreButton = dumb(function MoreButton(props) {
    if (props.items.length < props.total) {
        return (
            <button onClick={props.onLoadMore}>Load more</button>
        )
    }
})

export default class Sample extends React.Component {
    render() {
        const { items, total } = this.props
        return (
            <div>
                Results:
                <ul>
                    <ItemElements items={items} />
                </ul>
                <MoreButton
                    items={items}
                    total={total}
                    onLoadMore={this.onLoadMore.bind(this)} />
            </div>
        )
    }
}