tannerlinsley / use-react-hooks

Use hooks in classes in React 16.5+
https://codesandbox.io/s/wor3rxopv8
81 stars 4 forks source link
hooks javascript react react-hooks

use-react-hooks

use-react-hooks adds support for hooks in React v16.6!

Demos

Install

npm install --save use-react-hooks
# or
yarn add use-react-hooks

Usage

To use hooks in a functional compoennt, wrap it in useHooks then use any hook you want!

import React from "react";
import { useHooks, useState } from "use-react-hooks";

const App = useHooks(props => {
  const [count, setCount] = useState(0);
  return (
    <div>
      Count: {count}
      <button onClick={() => setCount(old => old + 1)}>Increment</button>
    </div>
  );
});

Documentation

The following hooks are available as named exports from the use-react-hooks package and follow the official React Hooks API here: https://reactjs.org/docs/hooks-reference.html

Additional Hooks

The following hooks are also provided for convenience:

License

MIT © tannerlinsley