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

@mjackson/my-react

v0.3.3

Published

A lightweight, drop-in replacement for React that avoids using ES6 classes and "this"

Downloads

2

Readme

myReact

myReact is a lightweight, drop-in replacement for React that avoids using ES6 classes and this.

In myReact, you don't create classes or extend React.Component. Instead, you just export a bunch of functions from a JavaScript module.

Instead of using this to access the component instance in methods, every method receives the instance as the first argument, usually called my (similar to how instance methods in Python always receive self as the initial argument). This helps developers avoid many common mistakes including:

  • forgetting to bind the appropriate this in event handler methods
  • getting the correct this inside inline event handlers in the render method
  • getting the correct this inside forEach/map in the render method

Other minor improvements to the ES6 class-based React component API include:

  • setupComponent instead of constructor; avoids the super boilerplate
  • getNextState instead of componentWillReceiveProps; automatically applies the return value to my.state
  • getElement instead of render; it's more descriptive

Now, I know, that sounds like a lot. Let's see what it looks like!

Here's how you might create a simple <TodoList> component:

import MyReact from "@mjackson/my-react"

const displayName = "TodoList"

const defaultProps = {
  title: "Todo List",
  initialItems: []
}

function setupComponent(my) {
  my.state = { items: my.props.initialItems }
}

function handleSubmit(my, event) {
  event.preventDefault()

  const todo = my.refs.todo

  my.setState({
    items: my.state.items.concat([todo.value])
  })

  todo.form.reset()
}

function getElement(my) {
  return (
    <div>
      <h1>{my.props.title}</h1>
      <ol>{my.state.items.map(item => <li>{item}</li>)}</ol>
      <form onSubmit={my.handleSubmit}>
        <input ref="todo" type="text" />
      </form>
    </div>
  )
}

export default {
  displayName,
  defaultProps,
  setupComponent,
  handleSubmit,
  getElement
}

Assuming the above code was saved in TodoList.js, you could use it just like any other React component (see Usage below):

import MyReact from "@mjackson/my-react"
import ReactDOM from "react-dom"
import TodoList from "./TodoList"

const node = document.getElementById("app")

ReactDOM.render(<TodoList />, node)

You can also put your own properties on the my object that aren't needed for rendering, like timers and references to in-flight XHR objects. Each time a lifecycle method is invoked for a given component, it receives the same instance.

Note in the following example how my.timer is set in componentDidMount and then cleaned up in componentWillUnmount.

import MyReact from "@mjackson/my-react"

const displayName = "Counter"

function setupComponent(my) {
  my.state = { count: 0 }
}

function componentDidMount(my) {
  my.timer = setInterval(() => {
    my.setState({ count: my.state.count + 1 })
  }, 1000)
}

function componentWillUnmount(my) {
  clearInterval(my.timer)
}

function getElement(my) {
  return <p>The current count is {my.state.count}</p>
}

export default {
  displayName,
  setupComponent,
  componentDidMount,
  componentWillUnmount,
  getElement
}

Installation

yarn add @mjackson/my-react

Usage

Assuming you're already using Babel for compiling JSX, you can just do:

import MyReact from "@mjackson/my-react"

// Tell Babel to transform JSX into MyReact.createElement calls
/** @jsx MyReact.createElement */

If you'd rather not put that comment in every file where you're using JSX, you can just put the following in your .babelrc:

{
  "plugins": ["transform-react-jsx", { "pragma": "MyReact.createElement" }]
}

Feedback

I'd love to get some feedback on this approach. Please feel free to reach out on Twitter or GitHub.

Thanks!