• Stars
    star
    150
  • Rank 247,323 (Top 5 %)
  • Language
    TypeScript
  • Created almost 5 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

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

More Repositories

1

htmr

Simple and lightweight (< 2kB) HTML string to React element conversion library
TypeScript
526
star
2

naskah

Bahasa pemrograman dengan sintaks Bahasa Indonesia (Programming language with Indonesian syntax) 🇮🇩
Rust
158
star
3

raw.macro

Read file contents at build time via babel-plugin-macros or SWC plugin.
JavaScript
149
star
4

katla

Indonesian version of Wordle
TypeScript
141
star
5

indihmm

Indihome real time twitter rant dashboard
TypeScript
69
star
6

aperture

Color theme for VSCode to help you focus
68
star
7

theme-in-css

Type-safe CSS custom properties (CSS variables) for theming purpose
TypeScript
65
star
8

vite-plugin-sloth

Fresh take on static site generation, using HTML-first approach to build website. Powered by ⚡️ Vite
HTML
44
star
9

oge

Page metadata as a service
HTML
26
star
10

atom-snazzy-clear-syntax

Modern, elegant, clean and consistent Atom syntax theme
CSS
13
star
11

asset-graph-webpack-plugin

Webpack plugin to easily get assets dependency graph based on entry point
JavaScript
13
star
12

smartass

Test your javascript knowledge
JavaScript
12
star
13

paper-editor

Medium editor clone, extracted from pveyes/paper
JavaScript
8
star
14

text-fragments

Text Fragments and Selection utilities
JavaScript
8
star
15

vcb

Multi-User, Multi-Room Video Streaming using WebRTC and Websocket
JavaScript
5
star
16

scarecrow

Tanifund scraper & notification service leveraging GitHub Actions Scheduling Workflow
JavaScript
5
star
17

octopub

GitHub public data as a service
Rust
4
star
18

makna

JavaScript
3
star
19

slack-robot-state

slack-robot plugin to enable state management
JavaScript
3
star
20

jest-multi-project-monorepo-coverage-bug

A small repro step for https://github.com/facebook/jest/issues/9323
JavaScript
2
star
21

imstore

Immutable in memory key-value store
JavaScript
2
star
22

pveyes

2
star
23

glg

Git log parser for easier analysis
JavaScript
1
star
24

byzantine

Istanbul json coverage parser
JavaScript
1
star
25

next.js-i18n-app-pages-hybrid

Reproduction case for routing issue in app/pages hybrid with i18n config
TypeScript
1
star
26

localhost

dotfiles, editor config, notes, and other development setup
Shell
1
star
27

talks

Tech talks & slides
JavaScript
1
star
28

next-swc-ssg-code-elimination

JavaScript
1
star