• Stars
    star
    181
  • Rank 212,110 (Top 5 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 5 years ago
  • Updated almost 2 years ago

Reviews

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

Repository Details

Add support for React Hooks in your class components. Live your own life and break the rules.

๐ŸŽฃ with-react-hooks ๐ŸŽฃ GitHub license npm version Coverage Status CircleCI Status PRs Welcome

Production Ready for Enterprise Applications

Don't let the React team control your life. Take back control.

I love hooks, but it kind of sucks that they can't be used in class components. But we can fix that! This package exposes an HOC withReactHooks, that allows any hook to be used inside of a class component.

This uses actual react hooks, not just an imitation. You can directly import and use the hooks provided by the react package.

๐Ÿ”ฅ๐Ÿ”ฅ๐Ÿ”ฅ Now featuring a new ๐Ÿ”ฅBlazing Fast๐Ÿ”ฅ implementation. This only adds one additional component to your tree.

Installing

You probably shouldn't, but if you want to:

$ npm install with-react-hooks

Usage

import React, { useState } from 'react';
import withReactHooks from 'with-react-hooks';

class Counter extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            count: 0
        };
    }

    render() {
        const [name, setName] = useState('');

        return (
            <div>
                {this.state.count}
                <button onClick={() => this.setState({ count: this.state.count + 1 })}>
                    +
                </button>

                <input value={name} onChange={(e) => setName(e.target.value)} />
            </div>
        );
    }
}

// Wrap with `withReactHooks` to add hook support:
export default withReactHooks(Counter);

Project Goals

When creating this project, I tried to keep these goals in mind:

  • Put hooks in classes no matter the cost.
  • Use real React hooks. No imitations, no passing props, no custom imports.
  • Support any custom hooks.
  • Support all normal class garbage.
  • Adding support to existing classes should just be one line of code (a single wrapper on the class).
  • For ease of debugging should maintain as much structure of the original component as possible.

More Repositories

1

ethereal-react

A suspense-compatible React library for Ethereum development
TypeScript
130
star
2

svelte-relay

Easily use GraphQL in Svelte, powered by the production-ready Relay runtime.
TypeScript
108
star
3

HostyHosting

A platform to easily manage and deploy your applications.
TypeScript
92
star
4

acorn-dynamic-import

JavaScript
45
star
5

jeopardy-bot

๐Ÿ’ฅ โ€‹A Slack bot for Jeopardy! games. ๐Ÿ’ฅ
TypeScript
31
star
6

relay-workshop-demo

TypeScript
19
star
7

text-me-maybe

TypeScript
18
star
8

iou-nft

Solidity
11
star
9

vanilla-tailwind

An experiment to implement a subset of TailwindCSS in vanilla-extract.
TypeScript
9
star
10

truwu

TypeScript
7
star
11

nextjs-prisma

TypeScript
7
star
12

graphql-rpc

TypeScript
7
star
13

uwu

TypeScript
6
star
14

subscriptions-transport-uws

๐Ÿ”ƒ A WebSocket client + server for GraphQL subscriptions (based on ยตWS)
JavaScript
5
star
15

annotated-prop-types

Add extra info to your React PropTypes.
JavaScript
4
star
16

characters

Just some characters
TypeScript
4
star
17

magic-api

Treat your APIs like the rest of your code, and just call functions!
TypeScript
3
star
18

react-yoga

JavaScript
3
star
19

typescript-talk

JavaScript
3
star
20

thrift-native

Native thrift bindings for Node.js
C++
3
star
21

stream-notes

Notes from my stream.
JavaScript
2
star
22

graphql-schema-subset

Generate subsets of your GraphQL Schema.
TypeScript
2
star
23

api-resource

Helpful wrappers for API interactions, based on fetch.
2
star
24

Enjo

Enyo Syntax, Jo UI
JavaScript
2
star
25

nytro-web3

2
star
26

potionseller

2
star
27

mcface

Descriptiony McDescriptionface
JavaScript
2
star
28

apollo-restify

Apollo Server 2 Restify Middleware
TypeScript
2
star
29

space-jam-ui

why?
TypeScript
1
star
30

walkie-talkie

You know what it is
JavaScript
1
star
31

chess-bot

A slack bot that plays chess.
JavaScript
1
star
32

jeopardy-bot-rewritten

A rewritten version of Jeopardy Bot, written live on stream: http://vapejuicejordan.rip
TypeScript
1
star
33

VapeJuiceJordan

1
star
34

frequency

Frequency Study
JavaScript
1
star
35

injectable.js

feature injection in javascript
JavaScript
1
star