proppy-preact
v1.3.1
Published
Preact integration with ProppyJS
Downloads
29
Readme
proppy-preact
Preact integration package for ProppyJS
Guide
Installation
With npm:
$ npm install --save proppy preact proppy-preact
Via unpkg CDN:
<script src="https://unpkg.com/proppy@latest/dist/proppy.min.js"></script>
<script src="https://unpkg.com/proppy-preact@latest/dist/proppy-preact.min.js"></script>
<script>
// available as `window.ProppyPreact`
</script>
Usage
First, setup your root component with providers data:
// components/Root.js
import { h } from 'preact';
import { ProppyProvider } from 'proppy-preact';
import MyComponent from './MyComponent';
const providers = {
foo: 'foo value',
};
export default function Root() {
return (
<ProppyProvider providers={providers}>
<MyComponent />
</ProppyProvider>
);
}
Now anywhere from your components tree, you can use the attach
higher-order component:
// components/MyComponent.js
import { h } from 'preact';
import { compose, withProps } from 'proppy';
import { attach } from 'proppy-preact';
const P = compose(
withProps((props, providers) => ({
foo: providers.foo,
})),
withProps({ bar: 'bar value' }),
);
function MyComponent(props) {
const { foo, bar } = props;
return <p></p>;
}
export default attach(P)(MyComponent);
API
ProppyProvider
For setting providers at Preact's context-level.
Example:
import { h } from 'preact';
import { ProppyProvider } from 'proppy-preact';
export default function Root() {
return (
<ProppyProvider providers={providers}>
<SomeOtherComponent />
</ProppyProvider>
);
}
attach
attach(P)(Component)
Higher-order component for attaching your Proppy factory to your Component.