-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
71 lines (60 loc) · 1.64 KB
/
index.js
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import React from "react";
import {createStore} from "redux";
import {Provider, connect} from "react-redux";
function add() {
return {type: "ADD"};
}
function remove() {
return {type: "REMOVE"};
}
function reducer(state=[], action) {
switch (action.type) {
case "ADD":
return state.concat({foo: {bar: Math.random()}});
case "REMOVE":
state = state.slice(0, -1);
console.log("REMOVE: state length is now " + state.length);
return state;
default:
return state;
}
}
class Item extends React.Component {
render() {
console.log("Item render: " + this.props.value);
return (
<span>{this.props.value}</span>
);
}
}
Item.propTypes = {
value: React.PropTypes.number.isRequired
};
Item = connect((state, componentProps) => {
console.log("Selecting index " + componentProps.index, state[componentProps.index]);
return {value: state[componentProps.index].foo.bar}
})(Item);
class App extends React.Component {
render() {
return (
<div>
<button onClick={this.props.add} >add</button>
<button onClick={this.props.remove} >remove</button>
<ul>
{this.props.items.map((v, i) => <li key={i}><Item index={i} /></li>)}
</ul>
</div>
);
}
}
App = connect(state => {
return {items: state};
}, {add, remove})(App);
const store = createStore(reducer);
React.render(
<div>
<Provider store={store}>
{() => <App />}
</Provider>
</div>
, document.getElementById("app"));