Git Product home page Git Product logo

retro-hooks's Introduction

Retro-Hooks

(work in progress...)

Are you stuck on an older version of react and unable to upgrade in order take advantage of hooks? This might be the repo for you.

this repo attempts to create a similar hooks api with some minor caveats and workarounds

min version of react tested on 16.0.0-alpha.6

core implemented hooks (incomplete)

- useState
- useEffect
- useRef
- useMemo
- useCallback
- useContext (wip)

additional tools

- createContext

small counter example (w/ react-native & typescript)

// example in react-native

import * as React from 'react';
import { View, Text, Button } from 'react-native';
import { withHooks, Hooks } from 'retro-hooks';

// wrap a functional react component with `withHooks` to access the hooks api

const MyComponent = withHooks<Props>(({ useState, useEffect, useRef }, props) => {

  const [counter, setCounter] = useState(0);

  useEffect(() => {
    console.log(`counter changed to ${counter}`);
  }, [counter]);

  return (
    <View>
      <Text>{counter}</Text>
      <Button onPress={() => setCounter(c => c + 1)} title="increment" />
    </View>
  );
});

useRedux implementation example (w/ react-native & typescript)

/* ./useRedux.ts */

import { Hooks } from 'retro-hooks';
import { getReduxStore, RootState } from './mystore';
import deepEquals from './mydeepequals';

export type StoreState = RootState;

// notice that the hooks object given by `withHooks` must be explicitly passed to custom hooks
export default <SubState>({ useState, useEffect }: Hooks, selectSubstate: (state: RootState) => SubState, inputs?: unknown[]): SubState => {
  const [substate, setSubstate] = useState(() => selectSubstate(getReduxStore().getState()));

  const checkForUpdates = (prevState: SubState) => {
    const nextSubstate = selectSubstate(getReduxStore().getState());
    if (!deepEquals(prevState, nextSubstate)) {
      setSubstate(nextSubstate);
      return nextSubstate;
    } else {
      return prevState;
    }
  };

  useEffect(() => {
    let prevState = substate;
    return getReduxStore().subscribe(() => prevState = checkForUpdates(prevState));
  }, inputs);

  return substate;
}


/* ./app.ts */

import useRedux from './useRedux.ts';

const MyApp = (_props: {}) => {
  const mySubState = useRedux(rootReduxState => rootReduxState.subReduxState);
  return <View>{mySubState}</View>;
}

retro-hooks's People

Contributors

kyunghoon avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.