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

@pveyes/use-less

v1.0.2

Published

React hooks that help you do what you already did, with more indirection

Downloads

6

Readme

use-less

React hooks that help you do what you already did, with more indirection

Warning: this package is ready for production use because of 1.x.x version

Install

npm install @pveyes/use-less

APIs

All the functionalities are available inside named import. Because it's written in typescript, you can be sure these hooks are free of bugs.

useProps

React already provide useState hooks, but what if you want to use props instead? use-less provides useProps hooks to get your actual props:

import { useProps } from '@pveyes/use-less';

function Component(props) {
  const actualProps = useProps(props);
  // you can finally use the actual component props
  return <div {...actualProps} />;
}

In cases where your props is computationally expensive, you can use lazy initializer, similar to how it works in useState

import { useProps } from '@pveyes/use-less';

function Component(expensiveProps) {
  const props = useProps(() => expensiveProps);
  // you can finally use the actual component props
  return <div {...props} />;
}

useConstructor

If you don't like the way React uses tuple for its state hooks and you feel like setting state on constructor is the way to go, you can use useConstructor hooks to do that.

import { useConstructor } from '@pveyes/use-less';

function Component() {
  // If you're feeling nostalgic, you can use Cyrillic character
  // to name your variable `thіs` without v8 yelling at you
  const thіs = useConstructor(function constructor() {
    this.state = {
      text: string;
    }
  });

  // It feels so good to use this.state & this.setState
  // RIGHT? RIGHT???
  return (
    <input
      value={thіs.state.text}
      onChange={e => thіs.setState({ text: '' })}
    />
  );
}

Yes, you need to use normal function, not arrow function.

useDerivedStateFromProps

Moving to React hooks means you lose one of the most powerful React API: getDerivedStateFromProps or gDSFP for short. Don't be afraid, we bring it back in use-less using useDerivedStateFromProps or uDSFP for short.

import { useDerivedStateFromProps } from '@pveyes/use-less';

// if you're familiar with redux, you'll be familiar with this as well
function mapPropsToState(props) {
  return {
    value: props.value,
    onChange: () => void 0,
  };
}

function Component(props) {
  const state = useDerivedStateFromProps(props, mapPropsToState);
  return <input value={state.value} onChange={state.onChange} />;
}

useRenderProps

With hooks, you see less and less render props pattern being used. use-less provides useRenderProps to help you cling to your old pattern:

import { useRenderProps } from '@pveyes/use-less';

function Component(props) {
  const renderProps = useRenderProps(props);
  return renderProps(props => <section {...props} />);
}

useHOC

Another thing that's missing since hooks era is Higher Order Component. One that was praised for being powerful is now starting to be abandoned. Fortunately, you can still use HOCs using useHOC hooks (no pun intended).

import { useHOC } from '@pveyes/use-less';
import withLegacy from './hoc';

function Component(props) {
  const renderHOC = useHOC(withLegacy);
  return renderHOC(hocProps => <div {...props} {...hocProps} />);
}

This is even better than just using HOC, there's no more props naming conflict! This is the power of composition between hooks, HOC and render props!

useGlobalContext

The main issue with React Context is you can only get value that the Provider gives you, or its default value. What if you want to access global value? With the rise of SSR, you need to be sure you call correct global console in both server and browser. With useGlobalContext you can access all global variable that exists in both environment.

It works in SSR and browser without any configuration!

import { useGlobalContext } from '@pveyes/use-less';

function Component(props) {
  const { console } = useGlobalContext();
  console.log('It works!');
  return null;
}

FAQ

  • Does it work with concurrent mode

    Yes, all this hooks should work in concurrent mode. Our example uses React.StrictMode to make sure it works with future version of React.

  • Can I really use this in production?

    Yes, version 1.x.x means it's already stable and ready to use in production

  • Why is it @pveyes/use-less and not use-less?

    Because there's already useless npm package, and npm doesn't allow package using similar name with existing package. If you want to donate the package name, I'll be happy.

  • Is this a joke?

    What do you think?

License

MIT