Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: React hooks #39

Open
fahad19 opened this issue Dec 4, 2018 · 0 comments
Open

Proposal: React hooks #39

fahad19 opened this issue Dec 4, 2018 · 0 comments
Labels

Comments

@fahad19
Copy link
Owner

fahad19 commented Dec 4, 2018

WIP branch exists here: https://github.com/fahad19/proppy/tree/proppy-react-hook

Background

React hooks will land soon in stable version, and currently already available in alpha version.

More info: https://reactjs.org/docs/hooks-intro.html

Set up

For the examples below, we will be using these providers:

import React from 'react';
import { ProppyProvider } from 'proppy-react';

const providers = {
  foo: 'foo value',
  bar: 'bar value',
};

export default function Root() {
  return (
    <ProppyProvider providers={providers}>
      <MyComponent />
    </ProppyProvider>
  );
}

And the sample factory:

import { compose, withProps, withState, shouldUpdate } from 'proppy';

const P = compose(
  withProps((props, providers) => {}),
  withState('counter', 'setCounter', 0),
  shouldUpdate(props => props.counter % 2 === 0),
);

Usage APIs

useProppy

Using Proppy factory inside Components:

import React from 'react';
import { useProppy } from 'x'; // new package

const P; // factory

export default function MyComponent(props) {
  const { counter, setCounter } = useProppy(P, props);

  return (
    <p onClick={() => setCounter(counter + 1)}>
      {counter}
    </p>
  );
}

useProviders

Access all the providers:

import React from 'react';
import { useProviders } from 'x';

export default function MyComponent(props) {
  const { foo, bar } = useProviders();

  return <p></p>;
}

useProvider

Access individual providers:

import React from 'react';
import { useProvider } from 'x';

export default function MyComponent(props) {
  const foo = useProvider('foo');

  return <p></p>;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant