51 lines
1.3 KiB
JavaScript
51 lines
1.3 KiB
JavaScript
// НЕ РАБОТАЕТ!
|
||
const React = require('react');
|
||
const Store = require('./Store.js');
|
||
|
||
// "react-redux connect()"-like example
|
||
var StoreListener = React.createClass({
|
||
componentDidMount: function()
|
||
{
|
||
Store.on(this.update);
|
||
},
|
||
componentWillUnmount: function()
|
||
{
|
||
Store.un(this.update);
|
||
},
|
||
update: function()
|
||
{
|
||
var newState = this.mapStateToProps(Store.data);
|
||
for (var i in newState)
|
||
{
|
||
if (this.state[i] != newState[i])
|
||
{
|
||
this.setState(newState);
|
||
return;
|
||
}
|
||
}
|
||
},
|
||
getInitialState: function()
|
||
{
|
||
return { ...this.initial, ...this.mapStateToProps(Store.data) };
|
||
},
|
||
render: function()
|
||
{
|
||
var props = { ...this.initial, ...this.props, ...this.state };
|
||
return React.createElement(this.wrappedComponent, props);
|
||
}
|
||
});
|
||
|
||
module.exports = function(component, map, initial)
|
||
{
|
||
var fn = function(props, context, updater)
|
||
{
|
||
StoreListener.call(this, props, context, updater);
|
||
this.wrappedComponent = component;
|
||
this.mapStateToProps = map;
|
||
this.initial = initial||{};
|
||
};
|
||
fn.prototype = Object.create(StoreListener);
|
||
fn.prototype.constructor = fn;
|
||
return fn;
|
||
};
|