connect-react-context
TypeScript icon, indicating that this package has built-in type declarations

0.0.0 • Public • Published

connect-react-context

like react-redux, connect react context to props. support typescript.

usage

import * as React from 'react';
import { connectContext } from 'connect-react-context';
import { joinContext, ContextValues } from 'join-react-context';

const helloContext = React.createContext('hello');
const worldContext = React.createContext('world');
type Contexts = [ typeof helloContext, typeof worldContext ];
const { Provider, Consumer } = joinContext<Contexts>([ helloContext, worldContext ]);

const App = () => (
    <Provider value={[ 'hello', 'world' ]}>
        <ConnectedComponent c='!'/>
    </Provider>
);

interface ComponentProps {
    a: string;
    b: string;
    c: string;
}
const Component: React.SFC<ComponentProps> = ({ a, b, c }) => (
    <div>{ a }, { b }{ c }</div>
);
const ConnectedComponent = connectContext<ContextValues<Contexts>, ComponentProps, 'a' | 'b'>(
    Consumer,
    ([ hello, world ], props) => ({ a: hello, b: world, ...props }),
)(Component);

Readme

Keywords

Package Sidebar

Install

npm i connect-react-context

Weekly Downloads

1

Version

0.0.0

License

Zlib

Unpacked Size

7.52 kB

Total Files

5

Last publish

Collaborators

  • disjukr