forked from preactjs/preact
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredux.jsx
48 lines (44 loc) · 1.09 KB
/
redux.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import { createElement } from 'preact';
import React from 'react';
import { createStore } from 'redux';
import { connect, Provider } from 'react-redux';
const store = createStore((state = { value: 0 }, action) => {
switch (action.type) {
case 'increment':
return { value: state.value + 1 };
case 'decrement':
return { value: state.value - 1 };
default:
return state;
}
});
class Child extends React.Component {
render() {
return (
<div>
<div>Child #1: {this.props.foo}</div>
<ConnectedChild2 />
</div>
);
}
}
const ConnectedChild = connect(store => ({ foo: store.value }))(Child);
class Child2 extends React.Component {
render() {
return <div>Child #2: {this.props.foo}</div>;
}
}
const ConnectedChild2 = connect(store => ({ foo: store.value }))(Child2);
export default function Redux() {
return (
<div>
<h1>Counter</h1>
<Provider store={store}>
<ConnectedChild />
</Provider>
<br />
<button onClick={() => store.dispatch({ type: 'increment' })}>+</button>
<button onClick={() => store.dispatch({ type: 'decrement' })}>-</button>
</div>
);
}