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

longwood-usestate

v0.3.5

Published

> Simple React useState and [context](https://reactjs.org/docs/context.html) > like state management library for > [Longwood](https://github.com/jehna/longwood)

Downloads

2

Readme

Longwood useState    Travis CI build status GitHub license npm version PRs Welcome

Simple React useState and context like state management library for Longwood

Example

createState

You can use the createState function to create a useState function:

const app = createRenderTarget()
const useState = createState(0)
const render = div(
  useState((state, setState) =>
    div(
      text(`Count: ${state}`),
      button({
        id: 'button',
        onclick: () => setState(state + 1),
        children: [text('+1')]
      })
    )
  )
)

The useState function exposes state and setState arguments to the callback, which you can use to manage your component-specific state.

▶️ Run in CodeSandbox.io

createContext

You can use createContext function to craete a context object with state:

const { provider, consumer } = createContext(0)
const render = provider(
  div(
    consumer((state, setState) =>
      div(
        text(`Count: ${state}`),
        button({
          onclick: () => setState(state + 1),
          children: [text('+1')]
        })
      )
    )
  )
)
render(app)

You can then modify the state and the provider automatically re-renders the DOM.

▶️ Run in CodeSandbox.io

TODO app example

You can check out a full TODO app example:

▶️ Run in CodeSandbox.io

Getting started (ES Modules)

longwood-usestate is available as ES module, so quickest way to get started is to import the module directly within your HTML page:

<html>
  <body>
    <div id="app"></div>
    <script type="module">
      import { div, button } from 'https://cdn.skypack.dev/longwood'
      import { createState } from 'https://cdn.skypack.dev/longwood-usestate'

      const useState = createState(0)
      const render = div(
        useState((state, setState) =>
          div(
            text(`Count: ${state}`),
            button({
              onclick: () => setState(state + 1),
              children: [text('+1')]
            })
          )
        )
      )
      render(document.getElementById('app'))
    </script>
  </body>
</html>

▶️ Run in CodeSandbox.io

This is literally all the code you'll need! No build tools needed, no extra steps, just save the code as a .html file and start hacking.

Getting started (npm)

You can install longwood-usestate to your project like a normal dependency within your project:

yarn add longwood longwood-usestate

Then you can import the package in your js file. For example if you're using Webpack, you can do:

import { div } from 'longwood'
import { useState } from 'longwood-usestate'

const useState = createState(0)
const render = div(
  useState((state, setState) =>
    div(
      text(`Count: ${state}`),
      button({
        onclick: () => setState(state + 1),
        children: [text('+1')]
      })
    )
  )
)
render(document.getElementById('app'))

Developing

You can use TDD for development by running:

yarn
yarn test --watch

This runs Jest, and the tests use JSDOM for asserting how DOM looks like.

Building

You can build the project by running:

yarn build

This builds the project into build/ directory.

Deploying

This project is automatically deployed to NPM by using Travis CI. All tagged versions are published when pushed.

Don't add tags by hand! Run:

yarn release

This will run an interactive deploy script to help you deploy the most recent version.

Contributing

This is a very early version of the project, and all feedback is welcome. Please open an issue before implementing, as the direction still needs some adjustments.

Licensing

The code in this project is licensed under MIT license.