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

kyklos

v1.0.0-beta.5

Published

Lifecycle hooks for React to abstract useEffect

Downloads

455

Readme

Kyklos

Pronunciation: /ˈkiːkləs/ (kee-klos)

Kyklos, derived from the Greek word κύκλος meaning "circle" or "cycle," embodies the concept of lifecycle in React components.

Kyklos is a lightweight React library that provides lifecycle hooks to abstract and simplify the use of useEffect. It offers a more intuitive and readable way to handle component lifecycle events in functional components.

Features

  • Simple and intuitive API
  • Lightweight with minimal overhead
  • TypeScript support
  • Easy to integrate into existing React projects

Installation

You can install Kyklos using your preferred package manager:

npm install kyklos

or

bun add kyklos

Usage

Kyklos exports three main hooks:

  1. useMounted: Runs a callback when the component is mounted.
  2. useUpdated: Runs a callback when the component is updated.
  3. useUnmounted: Runs a callback when the component is unmounted.
  4. $: A wrapper around useEffect that allows you to use jQuery-like syntax.

Here's how you can use these hooks in your React components:

import { useMounted, useUpdated, useUnmounted, $ } from "kyklos"

const MyComponent = () => {
    const [someState, setSomeState] = useState(0)

    useMounted(() => {
        console.log("Component mounted")
    })

    useUpdated(() => {
        console.log("Component updated. I will run on every re-render.")
    })

    useUpdated(() => {
        console.log(`someState updated to: ${someState}`)
    }, [someState])

    useUnmounted(() => {
        console.log("Component unmounted")
    })

    $(() => {
        console.log("Component mounted")
    }, [someState])

    return (
        <button onClick={() => setSomeState((prev) => prev + 1)}>
            Increment
        </button>
    )
}

API Reference

useMounted(callback: () => void)

Runs the provided callback function only when the component is mounted.

  • callback: A function to be executed on component mount.

useUpdated(callback: () => void, deps?: DependencyList)

Runs the provided callback function when the component is updated. This is different from useEffect because the callback will not be executed on the first render.

  • callback: A function to be executed on component update.
  • deps: (optional) Stateful dependencies that trigger the callback when they change. If they are present, the callback will only be executed if the dependencies have changed. Otherwise, it will be executed on every re-render.

useUnmounted(callback: () => void)

Runs the provided callback function only when the component is about to unmount. Used for cleanup.

  • callback: A function to be executed before the component unmounts.

$(callback: () => void, deps?: DependencyList)

A wrapper around useEffect that allows you to use jQuery-like syntax.

  • callback: A function to be executed on component mount.
  • deps: (optional) Stateful dependencies that trigger the callback when they change. If they are present, the callback will only be executed if the dependencies have changed. Otherwise, it will be executed on every re-render.

TypeScript Support

Kyklos is written in TypeScript and provides type definitions out of the box. No additional setup is required to use it in a TypeScript project.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Author

The one and only Gibson Murray

Acknowledgments

  • React team for trying to fit everything into useEffect (with love <3)
  • Vue for inspiring this project
  • jQuery for being the OGs
  • All contributors and users of Kyklos