-
-
Notifications
You must be signed in to change notification settings - Fork 2k
PropTypes for Preact #902
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
Comments
Hey @rumkin thanks for the issue. Preact offers an (undocumented) hook at the end of the import { options } from 'preact';
import PropTypes from 'prop-types';
function patchWithPropTypes(PatchingComponent) {
let name = PatchingComponent.displayName || PatchingComponent.name || 'component';
let render = PatchingComponent.prototype.render;
PatchingComponent.__patchedWithPropTypes = true;
if (render) {
PatchingComponent.prototype.render = function renderWithPropTypes(props, state, ctx) {
if (PatchingComponent.propTypes) {
for (let prop in props) {
PropTypes.checkPropTypes(PatchingComponent.propTypes, props, prop, name);
}
}
return render.call(this, props, state, ctx);
}
}
}
let nextVnode = options.vnode;
options.vnode = vnode => {
if (typeof vnode.nodeName==='function' && !vnode.nameNode.__patchedWithPropTypes) {
patchWithPropTypes(vnode.nodeName)
}
if (nextVnode) {
nextVode(vnode);
}
} I hope this helps you extend |
Also worth noting: |
Looks like we use @rumkin If you feel like the posted solutions don't solve your problem feel free to ping me to reopen this issue. |
Hi @marvinhagemeister, what do you mean when you say:
I've tried just importing
But there was no warning in the console. I only managed to make it work by using the solution proposed by @pl12133 with a custom hack (based on What is the recommended way going forward if I want to use Cheers ❤️ |
I stand corrected, we indeed don't support it out of the box in core :/ I'll try to get this into Preact X before it's out. |
Just an FYI: PropType checking will be possible in Preact X itself. That means that compat won't be needed just for PropTypes anymore. Merged the PR for it yesterday 🎉 |
Hot diggety! Thanks! |
We just released the alpha for Preact X which will check for |
@marvinhagemeister thanks for the info. If I'm not using |
it should be included by default |
@ForsakenHarmony I'm not seeing that in my current setup. Only if I manually require |
Oh sorry I misread, I thought you were using |
You can copy what we do there though, only enable it in dev, the check gets removed in a production build because of dead code elimination |
Hi @pirelenito.
Usage: Just add this to your project's index.js or import it as a simple file before the first preact render. |
Hi. I've realised PropTypes's alike extensible type checker which produce nice-looking report object. Instead of PropTypes which print's some output into console. And I want to add integration for preact. Is there any way to make it play together without patching preact itself?
It could be used like so:
Thanks for any advise.
P.S. Here it is.
The text was updated successfully, but these errors were encountered: