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

rehackt

v0.1.0

Published

A wrapper around React that will hide hooks from the React Server Component compiler

Downloads

8,203,964

Readme

Rehackt

This package is fairly advanced and is only intended for library developers that want to maintain high interop with Next.js server actions.

Rehackt invisibly wraps react so that you're able to use shared imports with react in server-side Next.js code without throwing an error to your users.

Explainer

Assume you have the following code in a Next.js codebase:

"use client"

import { useFormState } from "react-dom"
import someAction from "./action";

export const ClientComp = () => {
  const [data, action] = useFormState(someAction, "Hello client");

  return <form action={action}>
    <p>{data}</p>
    <button type={"submit"}>Update data</button>
  </form>
}
"use server"
// action.ts

import {data} from "./shared-code";

export default async function someAction() {
  return "Hello " + data.name;
}
// shared-code.ts
import {useState} from "react";

export const data = {
  useForm: <T>(val: T) => {
      useState(val)
  },
  name: "server"
}

While you're not intending to use data.useForm in your action.ts server-only file, you'll still receive the following error from Next.js' build process when trying to use this code:

./src/app/shared-code.ts
ReactServerComponentsError:

You're importing a component that needs useState. It only works in a Client Component but none of its parents are marked with "use client", so they're Server Components by default.
Learn more: https://nextjs.org/docs/getting-started/react-essentials

   ╭─[/src/app/shared-code.ts:1:1]
 1 │ import {useState} from "react";
   ·         ────────
 2 │ 
 3 │ export const data = {
 3 │   useForm: <T>(val: T) => {
   ╰────

Maybe one of these should be marked as a client entry with "use client":
./src/app/shared-code.ts
./src/app/action.ts

This is because Next.js statically analyzes usage of useState to ensure it's not being utilized in server-only code.

By replacing the import from react to rehackt:

// shared-code.ts
import {useState} from "rehackt";

export const data = {
  useForm: <T>(val: T) => {
      useState(val)
  },
  name: "server"
}

You'll no longer see this error.

Keep in mind, this does not enable usage of useState in server-only code, this just removes the error described above.

Further Reading

The following is a list of reading resources that pertain to this package: