-
Notifications
You must be signed in to change notification settings - Fork 49
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
Provide alternative to mixin #8
Comments
Did you ever solve this issue? I am still too new to solve this myself, but I am going to try none the less using https://github.com/timbur/react-mixin-decorator What are your thoughts? |
@motebotic That seems like a reasonable solution. Is that something you can use easily in your own project without requiring changes in I haven't yet decided how to provide an official alternative to mixin. One of the big downsides to me with react-mixin-decorator's approach is that it creates a higher-order component. While that works fine in most cases, it violates the principle of least surprise, because the decorated component actually becomes the HOC in practice. So if you decorate a component like I don't know the full ramifications of this, but I'm hoping best practices will emerge (or maybe they already have?) |
I've successfully used the mixins as decorators by using the The helper: import { BaseMixin } from 'react-rethinkdb';
const Rethinkable = sessionGetter => component => {
const proto = BaseMixin.call(component.prototype, sessionGetter);
const unmount = proto.componentWillUnmount;
proto.componentDidMount = function () {
this._isMounted = true;
};
proto.componentWillUnmount = function () {
this._isMounted = false;
unmount.call(this);
};
for (let key in proto) {
const _proto = component.prototype[key];
component.prototype[key] = function (...args) {
proto[key].call(this, ...args);
if (_proto) {
_proto.call(this, ...args);
}
}
}
};
export default Rethinkable; Usage: import React, { Component } from 'react';
import { r, QueryRequest } from 'react-rethinkdb';
import Rethinkable from '../helpers/rethinkable';
const session = component => component.props[name];
@Rethinkable(session)
class App extends Component {
observe (props, state) {
return {
turtles: new QueryRequest({
query: r.table('turtles'),
changes: true
})
};
}
render () {
return (
<ul>
{this.data.turtles.value().map(turtle => (
<li key={turtle.id}>{turtle.name}</li>
))}
</ul>
);
}
} Only thing needed modification in the codebase is https://github.com/mikemintz/react-rethinkdb/blob/master/src/util.js#L21 and needs to support checking for export const updateComponent = component => {
// Check for document because of this bug:
// https://github.com/facebook/react/issues/3620
if ((component._isMounted || component.isMounted()) && typeof document !== 'undefined') {
component.forceUpdate();
}
}; It doesn't brake the old method and should't be too much overhead. |
That seems pretty cool! Maybe since it seems like Then with that in place, we can add your decorator as a mixin-alternative. |
@mikemintz Any plans or news regarding implementing something like this anytime soon? |
@birkir this seems like a nice solution. I can't quite get it to work though; I get the error:
from this line:
Also, I notice that my Can you perhaps push a full working example to a branch of your fork? Thanks : ) |
@birkir, I duplicated the "tutorial" example in my fork, and applied your code it as I understand it: I get the same error in the browser console: Am I doing something wrong? Or does the react-rethink codebase need further modifications? |
BTW, I tried up updating all dependencies (including react) to their latest versions; behavior was unchanged. |
can someone give me working example for ES6 of this repo ??? as my project is completely built in react ES6 and i am unable to use this with.. if any one has any working example of react-rethinkdb in ES6 pls, provide me |
Mixins don't work with React components written as ES6 classes.
Let's provide something that will work like a superclass, higher order component, or ES7 decorator.
The text was updated successfully, but these errors were encountered: