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

PropTypes not working #627

Closed
marvinhagemeister opened this issue Aug 29, 2018 · 5 comments
Closed

PropTypes not working #627

marvinhagemeister opened this issue Aug 29, 2018 · 5 comments

Comments

@marvinhagemeister
Copy link
Member

Do you want to request a feature or report a bug?
Bug

What is the current behaviour?
PropType functions are never called

If the current behaviour is a bug, please provide the steps to reproduce.

Using the default preact-cli template:

import { h, Component } from 'preact';
import PropTypes from 'prop-types';

class Foo extends Component {
  static propTypes = {
	// Will never be called :/
    foo: PropTypes.string.isRequired
  }

  render() {
    return <h1>foo {this.props.foo}</h1>;
  }
}

export default class App extends Component {
	render() {
		return <div><Foo /></div>
	}
}

What is the expected behaviour?

Prop-Type validation should be called.

If this is a feature request, what is motivation or use case for changing the behaviour?

Please mention other relevant information.

  • Node version 10.0.9
  • npm version 6.0.0
  • Operating system MacOSX
  • CLI version: 2.2.1
  • Browser Chrome/Firefox
@lukeed
Copy link
Member

lukeed commented Aug 29, 2018

Is this in a production build? They are removed during build and kept within watch:

https://github.com/developit/preact-cli/blob/master/src/lib/babel-config.js#L28

@marvinhagemeister
Copy link
Member Author

@lukeed This issue occurs in development mode. The statements and prop-types library are included in the generated bundle, but the code for prop-types is never executed.

Digging deeper it seems like preact-compat should trigger the prop type checking. But even then when I use the Component export from there, the validation for prop-types is never run.

@ForsakenHarmony
Copy link
Member

have you tried importing "react"?

@marvinhagemeister
Copy link
Member Author

@ForsakenHarmony yep, switching to the react import doesn't help although the webpack config points to it being aliased to preact-compat.

Stepping through with a debugger it looks like the Component class is still used directly from preact instead of preact-compat. Therefore createClass which sets up the prop-type handling is never called. The only thing that's called from compat is the options.vnode hook.

@marvinhagemeister
Copy link
Member Author

Turns out that this is an issue with our process.env.NODE_ENV check in preact-compat. Made a PR there preactjs/preact-compat#504 .

Closing the issue here because it is not an issue with preact-cli itself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants