-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReactReduxExample.html
110 lines (93 loc) · 2.64 KB
/
ReactReduxExample.html
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<!DOCTYPE html>
<html>
<head>
<title>React Redux Example</title>
<script src="https://unpkg.com/react@16.3.0-alpha.1/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@16.3.0-alpha.1/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/redux@latest/dist/redux.min.js"></script>
<script src="https://unpkg.com/babel-standalone@6.15.0/babel.min.js"></script>
</head>
<!--React will mount here-->
<div id='reactapp'></div>
<body>
<!--React Code goes here, so Babel can transpile-->
<script type='text/babel'>
const assignmentReducer = (state=[], action) => {
if(action.type === 'ADD_ASSIGNMENT'){
return state.concat([action.assignment])
}
return state;
}
const store = Redux.createStore(
Redux.combineReducers({assignment: assignmentReducer})
);
store.subscribe(() => console.log(store.getState()))
function AssignmentList (props) {
const { assignment } = props;
const list = assignment.map((d) => <li key={d.name}>{d.name}</li>)
return (
<div>
<ul>
{list}
</ul>
</div>
)
}
class Assignment extends React.Component{
addAssignment = (event) => {
event.preventDefault();
const value = this.input.value
this.input.value = '';
this.props.store.dispatch({
type: 'ADD_ASSIGNMENT',
assignment: {
name: value,
class: 2019
}
})
}
render(){
return(
<div>
<h1>Assignment</h1>
<input
type='text'
placeholder='Add assignment'
ref={(input) => this.input = input} // instead of using sate, we grab from DOM
/>
<button onClick={this.addAssignment}>Add Assignment</button>
<hr/>
<AssignmentList assignment={this.props.assignment}/>
</div>
)
}
}
class App extends React.Component {
constructor(props){
super(props);
this.state = {
assignment: []
}
}
componentDidMount(){
this.props.store.subscribe(() => {
const { assignment } = this.props.store.getState();
this.setState(() => ({
assignment
}))
})
}
render(){
return (
<Assignment assignment={this.state.assignment} store={this.props.store}/>
)
}
}
// Adds react to dom
ReactDOM.render(
<App store={store}/>,
document.getElementById('reactapp')
)
</script>
</body>
</html>