feat: subscribe to new observables
This commit is contained in:
parent
d795abe5fd
commit
52695e5538
|
@ -48,7 +48,7 @@ class App extends React.Component {
|
|||
return (
|
||||
<div>
|
||||
<button onClick={this.createTrx}>Create Trx</button>
|
||||
{escrowObservable && <EscrowObservator escrow={escrowObservable} myCustomProperty="Test" />}
|
||||
<EscrowObservator escrow={escrowObservable} myCustomProperty="Test" />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@ import observe from "./observe";
|
|||
|
||||
const Escrow = props => {
|
||||
const { escrow, myCustomProperty } = props;
|
||||
if(!escrow) return <p>Loading...</p>;
|
||||
return (
|
||||
<ul>
|
||||
<li>
|
||||
|
|
|
@ -8,9 +8,9 @@ function observe(WrappedComponent) {
|
|||
subscriptions: {}
|
||||
}
|
||||
|
||||
componentDidMount() {
|
||||
Object.keys(this.props).forEach(prop => {
|
||||
if(isObservable(this.props[prop])){
|
||||
subscribeToProp = prop => {
|
||||
if(!isObservable(this.props[prop])) return;
|
||||
|
||||
const subscription = this.props[prop].subscribe(
|
||||
value => {
|
||||
this.setState({
|
||||
|
@ -33,7 +33,9 @@ function observe(WrappedComponent) {
|
|||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
componentDidMount() {
|
||||
Object.keys(this.props).forEach(this.subscribeToProp);
|
||||
}
|
||||
|
||||
componentWillUnmount() {
|
||||
|
@ -42,6 +44,16 @@ function observe(WrappedComponent) {
|
|||
});
|
||||
}
|
||||
|
||||
componentDidUpdate(prevProps) {
|
||||
Object.keys(prevProps).forEach(prop => {
|
||||
if(!prevProps[prop] && this.props[prop]){
|
||||
this.subscribeToProp(prop);
|
||||
}
|
||||
});
|
||||
|
||||
// TODO: check if prevProps and currProps are different, and unsubscribe from prevProp
|
||||
}
|
||||
|
||||
render() {
|
||||
return <WrappedComponent {...this.props} {...this.state.observedValues} />;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue