Preact integration package for ProppyJS
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>
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);
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(P)(Component)
Higher-order component for attaching your Proppy factory to your Component.