Skip to content

Instantly share code, notes, and snippets.

@ericclemmons
Last active December 19, 2015 00:48
Show Gist options
  • Save ericclemmons/5870876 to your computer and use it in GitHub Desktop.
Save ericclemmons/5870876 to your computer and use it in GitHub Desktop.
Browserify seems to not be able to reference the same module if there are different paths (because of subdirectories?) Previously: Difficulty with dynamic components in React. Form (works) -> Field (works) -> Text (fails with "Object [object Object] has no method 'value' ). Apparently the mixins don't get mixed
var FieldMixin = {
value: function() {
return 'default value';
}
};
var FieldMixin = require('../mixins/field-mixin'); // Notice the path (1 level)
var Fields = {
'text': require('./fields/text.jsx')
};
var Field = React.createClass({
mixins: [FieldMixin],
render: function() {
var field = Fields[this.props.type];
var component = field(this.props);
return (
<div class="row">
<label>Default value: {this.value()}</label>
{component}
</div>
);
}
});
var FieldMixin = require('../../mixins/field-mixin'); // Notice the path (2 levels)
var Text = React.createClass({
mixins: [FieldMixin],
render: function() {
return (
<input type="text" value={this.value()} />
);
}
});
// Request dynamic "text" template
var Form = React.createClass({
render: function() {
return (
<form>
<Field type="text" />
</form>
);
}
});
@ericclemmons
Copy link
Author

I'm using Browserify for pulling in require('field-mixin.js').

> console.log(Field().constructor.prototype);
__reactAutoBindMap: Object
constructor: function (initialProps, children) {
render: function () {
value: function (value) {
...
> console.log(Fields['text']().constructor.prototype);
...
constructor: function (initialProps, children) {
render: function () {
...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment