-
Notifications
You must be signed in to change notification settings - Fork 330
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
Support ES6/TypeScript classes in Stores and Action types #444
Comments
+1 |
+1 |
If it is of any use in the meantime, I've created a wrapper ES6 class for Reflux.createStore that I'm using to extend from. 'use strict';
import Reflux from 'reflux';
export default class RefluxStore {
constructor() {
this._store = Reflux.createStore({});
}
listenTo(action, handler) {
this._store.listenTo(action, handler);
}
listenToMany(actions) {
for (var action in actions) {
if (actions.hasOwnProperty(action)) {
let actionName = `${action}`;
let handlerName = `on${actionName.substring(0,1).toUpperCase()}${actionName.substring(1)}`;
this._store.listenTo(actions[action], (...args) => {
let handler = this[handlerName].bind(this);
if (handler) {
handler(...args);
}
})
}
}
}
listen(handler) {
this._store.listen(handler);
}
trigger(...args) {
this._store.trigger(...args);
}
} I can then create a store as follows: 'use strict';
import testActions from './testActions';
import RefluxStore from './RefluxStore'
class TestStore extends RefluxStore {
// Initial setup
constructor() {
super();
// Register testActions
this.listenToMany(testActions);
}
onShowWelcome() {
console.log(this);
this.trigger(true);
}
}
let testStore = new TestStore();
export default testStore; Note my refluxActions are created normally (I don't see much point wrapping that atm). I'm using this in React Native so I also created a RefluxComponent class I can extend from in my React Native modules instead of Component. 'use strict';
import React, { Component } from 'react-native';
export default class RefluxComponent extends Component {
constructor(props) {
super(props);
this._refluxSubscriptions = [];
}
listenTo(store, handler) {
this._refluxSubscriptions.push(
store.listen(handler);
);
}
componentWillUnmount() {
super.componentWillUnmount();
for (let subscriptionUnsubscribe of this._refluxSubscriptions) {
subscriptionUnsubscribe();
}
}
}; I can then simple create a component as follows: 'use strict';
import React, {
View,
Image,
Text
} from 'react-native';
import RefluxComponent from './RefluxComponent';
import testStore from './testStore';
import testActions from './testActions';
export default class Welcome extends RefluxComponent {
constructor(props) {
super(props);
this.listenTo(testStore, this._handleStore)
testActions.test();
}
_handleStore() {
console.log("Store Event");
}
render() {
return (
<View style={this.props.styles.blueContainer}>
<Image source={require('./assets/logo.png')} />
<Text style={this.props.styles.welcomeText}>
Welcome
</Text>
</View>
);
}
} This is just thrown together quickly so there is probably a cleverer/more efficient way to do this. It may or may not serve your purpose but I tested it in React Native 0.21 and it is working fine (certainly for my own use cases). Also note I have only wrapped the methods I'm using and Mixins with ES6 are a whole other problem. |
The ES6 API now has |
And Reflux.Component does not build with Typescript because @types/reflux does not include it
if i "Go to definition" of that "reflux" this is the output :
|
Their definitions seem a little out of date. We're currently on There is an open issue here on Reflux for anyone that wishes to help maintain typescript definitions. If you'd prefer that the definitions on that repo be kept up, then an issue on that repo would be appropriate. |
Be able to use ES6 or TypeScript class as a Reflux Store/Action.
The exception "Uncaught TypeError: Property description must be an object: undefined" is raised in utils.js line 44. The propertyDescriptor variable is null when the prop is a function and that function is in the source prototype instead of the object itself.
The code below is a poor initial attempt to use a TypeScript class as a Reflux store.
Ideally, we would be able to do this in TypeScript. I'm assuming getting this to work with ES6 classes will naturally also work in TypeScript.
The text was updated successfully, but these errors were encountered: