Skip to content

Instantly share code, notes, and snippets.

@simon-robertson
Created November 2, 2018 12:11
Show Gist options
  • Save simon-robertson/3756c77c9cce7255290491e8d29ff8c6 to your computer and use it in GitHub Desktop.
Save simon-robertson/3756c77c9cce7255290491e8d29ff8c6 to your computer and use it in GitHub Desktop.
//
// app/state/TestCounter.js
//
// Contains partial application state, a bit like a small Redux store.
// It is a standard class with static fields and methods.
//
import { store } from 'app/decorators';
@store
class TestCounter {
static counter = 0;
static increaseCounter(increment = 1) {
return {
counter: this.counter + increment
};
}
}
export { TestCounter };
//
// app/display/Test.js
//
// A standard React component that subscribes to the TestCounter store.
//
// The component will automatically subscribe to, and unsubscribe from, the
// store when the component is mounted and unmounted.
//
import React, { Component } from 'react';
import { TestCounter } from 'app/state/TestCounter';
import { subscribe } from 'app/decorators';
@subscribe(TestCounter)
class Test extends Component {
componentDidMount() {
console.log('Test component mounted');
}
render() {
return (
<div onClick={ Layout.increaseCounter() }>
<h1>Epic state test, { Layout.counter }</h1>
</div>
);
}
}
export { Test };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment