-
Notifications
You must be signed in to change notification settings - Fork 3
/
interesting.js
360 lines (285 loc) · 8.57 KB
/
interesting.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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
//index.js
import React from 'react';
import { render } from 'react-dom';
import { Provider } from 'react-redux';
import { Router, hashHistory } from 'react-router';
import { syncHistoryWithStore } from 'react-router-redux';
import routes from './routes';
import configureStore from './store/configureStore';
import './app.global.css';
const store = configureStore();
const history = syncHistoryWithStore(hashHistory, store);
render(
<Provider store={store}>
<Router history={history} routes={routes} />
</Provider>,
document.getElementById('root')
);
///////////////////////////////////////////////////////////////////////////
//configureStore.production.js
import { createStore, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
import { hashHistory } from 'react-router';
import { routerMiddleware } from 'react-router-redux';
import rootReducer from '../reducers';
const router = routerMiddleware(hashHistory);
const enhancer = applyMiddleware(thunk, router);
export default function configureStore(initialState) {
return createStore(rootReducer, initialState, enhancer);
}
///////////////////////////////////////////////////////////////////////////
//configureStore.development.js
import { createStore, applyMiddleware, compose } from 'redux';
import thunk from 'redux-thunk';
import { hashHistory } from 'react-router';
import { routerMiddleware, push } from 'react-router-redux';
import createLogger from 'redux-logger';
import rootReducer from '../reducers';
import * as counterActions from '../actions/counter';
const actionCreators = {
...counterActions,
push,
};
const logger = createLogger({
level: 'info',
collapsed: true
});
const router = routerMiddleware(hashHistory);
const enhancer = compose(
applyMiddleware(thunk, router, logger),
window.devToolsExtension ?
window.devToolsExtension({ actionCreators }) :
noop => noop
);
export default function configureStore(initialState) {
const store = createStore(rootReducer, initialState, enhancer);
if (window.devToolsExtension) {
window.devToolsExtension.updateStore(store);
}
if (module.hot) {
module.hot.accept('../reducers', () =>
store.replaceReducer(require('../reducers')) // eslint-disable-line global-require
);
}
return store;
}
///////////////////////////////////////////////////////////////////////////
//routes.js
import React from 'react';
import { Route, IndexRoute } from 'react-router';
import App from './containers/App';
import HomePage from './containers/HomePage';
import CounterPage from './containers/CounterPage';
export default (
<Route path="/" component={App}>
<IndexRoute component={HomePage} />
<Route path="/counter" component={CounterPage} />
</Route>
);
///////////////////////////////////////////////////////////////////////////
// actions/counter.js
export const INCREMENT_COUNTER = 'INCREMENT_COUNTER';
export const DECREMENT_COUNTER = 'DECREMENT_COUNTER';
export function increment() {
return {
type: INCREMENT_COUNTER
};
}
export function decrement() {
return {
type: DECREMENT_COUNTER
};
}
export function incrementIfOdd() {
return (dispatch, getState) => {
const { counter } = getState();
if (counter % 2 === 0) {
return;
}
dispatch(increment());
};
}
export function incrementAsync(delay = 1000) {
return dispatch => {
setTimeout(() => {
dispatch(increment());
}, delay);
};
}
///////////////////////////////////////////////////////////////////////////
// reducers/index.js
import { combineReducers } from 'redux';
import { routerReducer as routing } from 'react-router-redux';
import counter from './counter';
const rootReducer = combineReducers({
counter,
routing
});
export default rootReducer;
///////////////////////////////////////////////////////////////////////////
// reducers/counter.js
//
import { INCREMENT_COUNTER, DECREMENT_COUNTER } from '../actions/counter';
export default function counter(state = 0, action) {
switch (action.type) {
case INCREMENT_COUNTER:
return state + 1;
case DECREMENT_COUNTER:
return state - 1;
default:
return state;
}
}
///////////////////////////////////////////////////////////////////////////
// containers/App.js
//////////////////
import React, { Component, PropTypes } from 'react';
export default class App extends Component {
static propTypes = {
children: PropTypes.element.isRequired
};
render() {
return (
<div>
{this.props.children}
</div>
);
}
}
///////////////////////////////////////////////////////////////////////////
// containers/HomePage.js
//////////////////
import React, { Component } from 'react';
import Home from '../components/Home';
export default class HomePage extends Component {
render() {
return (
<Home />
);
}
}
///////////////////////////////////////////////////////////////////////////
// containers/CounterPage.js
//////////////////
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import Counter from '../components/Counter';
import * as CounterActions from '../actions/counter';
function mapStateToProps(state) {
return {
counter: state.counter
};
}
function mapDispatchToProps(dispatch) {
return bindActionCreators(CounterActions, dispatch);
}
export default connect(mapStateToProps, mapDispatchToProps)(Counter);
///////////////////////////////////////////////////////////////////////////
// containers/Counter.js
//////////////////
import React, { Component, PropTypes } from 'react';
import { Link } from 'react-router';
import styles from './Counter.css';
class Counter extends Component {
static propTypes = {
increment: PropTypes.func.isRequired,
incrementIfOdd: PropTypes.func.isRequired,
incrementAsync: PropTypes.func.isRequired,
decrement: PropTypes.func.isRequired,
counter: PropTypes.number.isRequired
};
render() {
const { increment, incrementIfOdd, incrementAsync, decrement, counter } = this.props;
return (
<div>
<div className={styles.backButton}>
<Link to="/">
<i className="fa fa-arrow-left fa-3x" />
</Link>
</div>
<div className={`counter ${styles.counter}`}>
{counter}
</div>
<div className={styles.btnGroup}>
<button className={styles.btn} onClick={increment}>
<i className="fa fa-plus" />
</button>
<button className={styles.btn} onClick={decrement}>
<i className="fa fa-minus" />
</button>
<button className={styles.btn} onClick={incrementIfOdd}>odd</button>
<button className={styles.btn} onClick={() => incrementAsync()}>async</button>
</div>
</div>
);
}
}
export default Counter;
///////////////////////////////////////////////////////////////////////////
// containers/Home.js
//////////////////
import React, { Component } from 'react';
import { Link } from 'react-router';
import styles from './Home.css';
export default class Home extends Component {
render() {
return (
<div>
<div className={styles.container}>
<h2>Home</h2>
<Link to="/counter">to Counter</Link>
</div>
</div>
);
}
}
//
import React from 'react'
import ReactDOM from 'react-dom'
import { createStore } from 'redux'
import Counter from './components/Counter'
import counter from './reducers'
const store = createStore(counter)
const rootEl = document.getElementById('root')
const render = () => ReactDcom.render(
<Counter
value={store.getState()}
onIncrement={() => store.dispatch({ type: 'INCREMENT '})} />
)
render()
store.subscribe(render)
//
import React from 'react'
import { render } from 'react-dom'
import { createStore } from 'redux'
import { Provider} from 'react-redux'
import App from './componets/App'
const store = createStore(reducer)
render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById('root')
)
//HIGHER ORDER COMPONENT
import { connect } from 'react-redux'
export defaul function(ComposedComponent) {
class Authentication extends Component {
render() {
return <ComposedComponent {...this.props} />
}
}
function mapStateToProps(state) {
return { autheticated: state.authenticated };
}
return connect(mapStateToProps)(Authentication)
}
import Authentication // this is my Hoc
import Resources // The componenet I want to wrap
const ComposedComponent = Authentication(Resources);
<ComposedComponent />
//HOC IS A FUNCTION CALLED WITH ANOTHER COMP
//to get access to context you need to define them.
static contextTypes = {
router: React.PropTypes.object
}