我试图使用以下React-Reflux代码:
var TimeStore = Reflux.createStore({
listenables: [TimeActions],
onTick: function(tick) {
....
}
})
var Watch = React.createClass({
mixins: [Reflux.connect(TimeStore, 'tick')],
...
我不确定如何使用react-decorator转换它。这就是我将其转化为:
const SomeDecorator = MixinDecorator(
'TimerActions', // displayName
Reflux.connect(TimeStore, 'tick')
);
@SomeDecorator
class Watch extends React.Component {
...
它与babel一起编译(stage
设置为0
),但不能很好地工作。有任何建议如何解决这个问题?此外,是否有可能es6-ify商店?
答案 0 :(得分:8)
完全跳过mixins。
class AppCtrlRender extends Component {
binder(...methods) { methods.forEach( (method) => this[method] = this[method].bind(this) ); }
render() {
var data = this.state.Data;
data = JSON.stringify(data, null, 2);
var data2 = this.state.Data2;
data2 = JSON.stringify(data2, null, 2);
var data3 = this.state.Data3;
data3 = JSON.stringify(data3, null, 2);
return (
<div id='AppCtrlSty' style={AppCtrlSty}>
React 1.3 ReFlux with WebSocket<br/><br/>
{data}<br/><br/>
Data2: {data2}<br/><br/>
Data3: {data3}<br/><br/>
</div>
);
}
}
function getState() {
return {
Data: BasicStore.getData(),
Data2: BasicStore.getData2(),
Data3: BasicStore.getData3()
};
};
export default class AppCtrl extends AppCtrlRender {
constructor() {
super();
this.state = getState();
this.binder('storeDidChange');
}
componentDidMount() { this.unsubscribe = BasicStore.listen(this.storeDidChange); }
componentWillUnmount() { this.unsubscribe(); }
storeDidChange() { this.setState(getState()); }
}
答案 1 :(得分:2)
对于新的Reflux ES6 API,您的OP中代码的正确翻译将如下所示:
var TimeActions = Reflux.createActions(['tick']);
class TimeStore extends Reflux.Store
{
constructor()
{
super();
this.listenables = TimeActions;
// whatever state you want to store should
// now be on a state property in the store
this.state = { ticks: 0 };
}
onTick(tick)
{
// now update values in your store via setState
this.setState({ ticks: this.state.ticks+1 });
}
}
class Watch extends Reflux.Component
{
constructor(props)
{
super(props);
// set this.store to the store class itself
this.store = TimeStore;
}
render()
{
// from now any any changes to the store will automatically
// reflect in your component's state.
return <p>{this.state.ticks} ticks...</p>;
}
}
工作的JSFiddle在这里:https://jsfiddle.net/uomkhbzj/