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

unstated-retro

v1.0.0

Published

615 bytes to bridge the gap from unstated to unstated-next

Downloads

4

Readme

Unstated Retro

Retrofit your existing unstated container. Feels like unstated-next. Bridge the gap until you can swap.

Motivation

unstated-next is great for new projects, but if you've been using unstated for awhile, you probably already have some containers that aren't quick to rewrite and replace.

This package seeks to bridge that gap via a "child-first" migration. You can start rewriting child components to use hooks, and then eventually rewrite your container in the style of unstated-next. It aims to be API compatible with unstated-next for a smooth migration from (unstated + retro) to pure unstated-next.

Comparison to unstated and unstated-next

| | unstated | unstated-next | unstated-retro | |---------------|--------------|---------------|-----------------------| | Container | class CounterContainer extends Container | let Container = createContainer(customHook) | let RetroContainer = createRetroContainer(CounterContainer) | | Provider | <Provider> | <Container.Provider> | <RetroContainer.Provider> | | Subscribe | <Subscribe> | Container.useContainer() | RetroContainer.useContainer() | | Tunnel | - | - | <RetroContainer.Tunnel> | | React Version | ^15.0 | ^16.8 | ^16.8 |

The way to inject containers in unstated-retro matches the style of unstated-next.

| | unstated | unstated-next | unstated-retro | |---------------|--------------|---------------|-----------------------| | What is provided | Any Container class | The customHook passed to createContainer | The Container passed to createRetroContainer | | Inject an instance | <Provider inject={[instance]}> docs | - | createContainer(instance) |

Install

npm install --save unstated-retro

Example

import React, { useState } from "react"
import { render } from "react-dom"
import { Container } from 'unstated';
import { createRetroContainer } from "unstated-retro"

type CounterState = {
  count: number,
}

class CounterContainer extends Container<CounterState> {
  state = {
    count: 0,
  }

  increment() {
    this.setState({ count: this.state.count + 1 })
  }

  decrement() {
    this.setState({ count: this.state.count - 1 })
  }
}

let RetroContainer = createRetroContainer(CounterContainer)

function CounterDisplay() {
	let counter = RetroContainer.useContainer()
	return (
		<div>
			<button onClick={counter.decrement}>-</button>
			<span>{counter.count}</span>
			<button onClick={counter.increment}>+</button>
		</div>
	)
}

function App() {
	return (
		<RetroContainer.Provider>
			<CounterDisplay />
			<CounterDisplay />
		</RetroContainer.Provider>
	)
}

render(<App />, document.getElementById("root"))

API

createRetroContainer(ContainerOrInstance)

class CounterContainer extends Container<CounterState> {
  state = {
    count: 0,
  }

  increment() {
    this.setState({ count: this.state.count + 1 })
  }

  decrement() {
    this.setState({ count: this.state.count - 1 })
  }
}

let RetroContainer = createRetroContainer(CounterContainer)
// RetroContainer === { Provider, Tunnel, useContainer }

Container.useContainer()

function ChildComponent() {
  let input = RetroContainer.useContainer()
  return <input value={input.value} onChange={input.onChange} />
}

useContainer is designed to match the style of unstated-next

<Container.Tunnel>

let RetroContainer = createRetroContainer(CounterContainer)
// RetroContainer === { Provider, Tunnel, useContainer }

function ParentComponent() {
  return (
    <Provider>
      <RetroContainer.Tunnel>
	<CounterDisplay />
	<CounterDisplay />
      </RetroContainer.Tunnel>
      <CounterDisplay />
    </Provider>
  )
}

Bridges the context from a wrapper Provider via a Tunnel to a child using useContainer

<Container.Provider>

function ParentComponent() {
  return (
    <RetroContainer.Provider>
      	<CounterDisplay />
    </RetroContainer.Provider>
  )
}

Replaces <Provider/> from unstated.

<Container.Provider> injected instance

const counter = new CounterContainer(); // Global instance
let RetroContainer = createRetroContainer(counter)
// RetroContainer === { Provider, Tunnel, useContainer }

function ParentComponent() {
  return (
    <RetroContainer.Provider>
      <CounterDisplay />
    </RetroContainer.Provider>
  )
}

See also passing your own instances in unstated

How do I use an existing unstated container?

I need new shared state using an existing container

Use <RetroContainer.Provider> from unstated-retro in your new parent components, and useContainer in your new child components.

I am building a new child component

Use <RetroContainer.Tunnel> from unstated-retro in your existing parent components, and useContainer in your new child components.

I want to slowly migrate

  1. Use unstated-retro
    • Create a RetroContainer with createRetroContainer(LegacyContainer)
    • Add <RetroContainer.Tunnel> from unstated-retro in your existing parent components
  2. Start writing new child components using useContainer.
  3. Replace <Subscribe/>
    • Migrate existing child components from <Subscribe/> to useContainer.
  4. Replace `
    • Confirm all child components use useContainer instead of <Subscribe/>
    • Swap from <Provider><RetroContainer.Tunnel> to just <RetroContainer.Provider>
  5. Migrate to unstated-next
    • Confirm all parent components use <RetroContainer.Provider> instead of <Provider>
    • Rewrite your LegacyContainer as a hook
    • Switch from createRetroContainer to createContainer

I'm building something completely new

Don't use this library, use unstated-next. Celebrate that you're not bogged down by supporting legacy unstated containers.