ghost-portal/examples/tut-004.md
Jaco Greeff 3fb85094a6
Example & tutorial (#127)
* Add tutorial step 001

* Update LICENSE header

* Query storage via HOC

* Tut 003, read proposals

* Tut 004 replacing HOC

* Pull Intentions & Proposals together

* Add final image

* Cleanup unused methods

* Source links

* Remove unused var
2018-07-09 13:38:31 +02:00

45 lines
1.3 KiB
Markdown

# storage via API (part II)
[Previously](tut-002.md) we queried intentions via HOC, then [followed up](tut-003.md) with querying via API. Here we will replace our previous HOC component with the same storage queries.
# the component
Here we extend the component from the previous section, adding a subscription to intentions to create our [new component](../packages/app-example/src/comp-004.tsx) -
```js
class Comp extends React.PureComponent<ApiProps, State> {
...
componentDidMount () {
this.setState({
subscriptions: [
this.subscribeIntentions(),
this.subscribeProposals()
]
});
}
subscribeIntentions () {
const { api } = this.props;
const key = createStorageKey(intentionsMethod)();
const transform = storageTransform(intentionsMethod);
return api.state
.getStorage(key)
.subscribe((value) => {
this.setState({
intentions: (transform(value, 0) as any[]).map(encodeAddress)
});
});
}
...
}
```
# walk-through
This subscription is much simpler than what was found before, however it follows the same basic approach - query, transform - in this case the transform is the same as the one applied even for the HOC component.
# next steps
[Next up](tut-005.md), we will be using the actual data we have collected to render something useful on-screen.