Building our components

Finally, having fully defined our store plus the actions that will be dispatched and the reducer that will process them, we can finish quickly by defining our components. Our Counter component will have text, the counter value, and a few buttons. Note that we are receiving count (the counter value) as a prop, and we also have a dispatch() function as yet another prop:

// Source file: src/counterApp/counter.component.js

/* @flow */

import React from "react";
import { PropTypes } from "prop-types";

import {
increment,
decrement,
reset,
CounterAction
} from "./counter.actions.js";

export class Counter extends React.PureComponent<{
count: number,
dispatch: CounterAction => any
}> {
static propTypes = {
count: PropTypes.number.isRequired,
dispatch: PropTypes.func.isRequired
};

onAdd1 = () => this.props.dispatch(increment(1));
onSub2 = () => this.props.dispatch(decrement(2));
onReset = () => this.props.dispatch(reset());

render() {
return (
<div>
Value: {this.props.count}
<br />
<button onClick={this.onAdd1}>Add 1</button>
<button onClick={this.onSub2}>Subtract 2</button>
<button onClick={this.onReset}>Reset</button>
</div>
);
}
}

Each button dispatches an action that was created by the action creators that we saw earlier. 

We need a second component. The ClicksDisplay component is even simpler! We receive the total number of clicks as a prop, and we simply display it:

// Source file: src/counterApp/clicksDisplay.component.js

/* @flow */

import React from "react";
import { PropTypes } from "prop-types";

export class ClicksDisplay extends React.PureComponent<{
clicks: number
}> {
static propTypes = {
clicks: PropTypes.number.isRequired
};

render() {
return <div>Clicks so far: {this.props.clicks}</div>;
}
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.81.240