New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details
Socket
Book a DemoSign in
Socket

react-polyfill-hooks

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-polyfill-hooks

React hooks polyfill

latest
Source
npmnpm
Version
1.1.9
Version published
Maintainers
1
Created
Source

react-polyfill-hooks

Polyfill for React new hooks api

Support

>= react@0.14.9

Usage

Just use withHooks to wrap your component, everything is just like the new React.

import {
  withHooks,
  useState,
  useEffect,
  useReducer,
  useCallback,
  useRef
} from 'react-polyfill-hooks';

const App = withHooks((props) => {
  const [count, setCount] = useState(props.count || 0);
  const inputRef = useRef();

  useEffect(() => {
    console.log(count);
  });

  useEffect(() => {
    inputRef.current.focus();
    return () => {
      inputRef.current.blur();
    };
  }, []);

  return (
    <div>
      <p>{count}</p>
      <input ref={inputRef} />
      <button onClick={() => setCount(count + 1)}>+</button>
      <button onClick={() => setCount(count - 1)}>-</button>
    </div>
  );
});

function reducer(state, action) {
  switch (action.type) {
    case 'add':
      return { count: state.count + 1 };
    case 'minus':
      return { count: state.count - 1 };
    default:
      return state;
  }
}

const ReduxApp = withHooks((props) => {
  const [state, dispatch] = useReducer(reducer, { count: 0 });
  const add = useCallback(() => dispatch({ type: 'add' }), []);
  const minus = useCallback(() => dispatch({ type: 'minus' }), []);

  return (
    <div>
      <p>{state.count}</p>
      <button onClick={add}>+</button>
      <button onClick={minus}>-</button>
    </div>
  );
});

For more usage, see https://reactjs.org/docs/hooks-reference.html

Others

You can use it with react-polyfill-ref and react-polyfill-context

Keywords

React

FAQs

Package last updated on 15 Apr 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts