Skip to content

Commit 4f540c7

Browse files
committed
fix first hack - store is no longer global. Please see reduxjs/react-redux#108 for more details
1 parent 28bb08e commit 4f540c7

File tree

3 files changed

+22
-20
lines changed

3 files changed

+22
-20
lines changed

src/client.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
// Client (i.e. frontend) application entry point (i.e. main)
22
import React from 'react';
3-
import {Provider} from 'react-redux';
43
import {render} from 'react-dom';
54
import { Router, Route, hashHistory, IndexRoute } from 'react-router'
65

6+
import { createStore } from 'redux';
7+
import {Provider} from 'react-redux';
8+
import reducer from './reducers/reducer.js'
9+
710
import App from './components/app.js';
811
import Home from './components/home.js';
912
import Catalog from './components/catalog/catalog.js';
@@ -26,7 +29,7 @@ import ProductPage from './components/product_page.js';
2629
// which does a smart diff and touches the DOM only if it needs to).
2730

2831

29-
var store = require('./store.js');
32+
var store = createStore(reducer);
3033

3134
render(
3235
<Provider store={store}>

src/components/catalog/catalog.js

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,30 @@ import SearchBar from './search_bar.js';
44

55
require('isomorphic-fetch');
66

7-
var reduxStore = require('../../store.js');
8-
97

108
// A component that stores data (as its internal state).
119
// The data is passes to view-only component(s) as properties.
1210
// The render() method simply sets the properties of view-only components.
1311
export default class Catalog extends React.Component {
1412

1513

16-
constructor(){
17-
super();
18-
reduxStore.subscribe(() => {
14+
constructor(props, context){
15+
super(props, context);
16+
this.store = this.context.store;
17+
this.shouldDisplay = this.shouldDisplay.bind(this);
18+
19+
this.store.subscribe(() => {
1920
// FIXME: We need to use render(), not forceUpdate().
2021
// See comment in src/client.js
2122
this.forceUpdate();
2223
});
2324

24-
this.shouldDisplay = this.shouldDisplay.bind(this);
25+
2526
}
2627

2728

2829
shouldDisplay(item){
29-
let t = reduxStore.getState().filterText.toLowerCase();
30+
let t = this.store.getState().filterText.toLowerCase();
3031
return item.brand.toLowerCase().includes(t) ||
3132
item.name.toLowerCase().includes(t);
3233
}
@@ -36,7 +37,7 @@ export default class Catalog extends React.Component {
3637
fetch(this.props.getUrl)
3738
.then(res => res.json())
3839
.then(json => {
39-
reduxStore.dispatch({type: 'ITEMS_UPDATED', items: json.items});
40+
this.store.dispatch({type: 'ITEMS_UPDATED', items: json.items});
4041
})
4142
.catch(err => {
4243
// TODO: Change the state of this component, to indicate an error
@@ -47,9 +48,9 @@ export default class Catalog extends React.Component {
4748

4849

4950
render() {
50-
let displayedItems = reduxStore.getState().items.filter(this.shouldDisplay);
51+
let displayedItems = this.store.getState().items.filter(this.shouldDisplay);
5152
let onTextChanged = (t) => {
52-
reduxStore.dispatch({type: 'FILTER_TEXT_CHANGED', filterText: t});
53+
this.store.dispatch({type: 'FILTER_TEXT_CHANGED', filterText: t});
5354
};
5455

5556
return (
@@ -71,3 +72,9 @@ Catalog.propTypes = {
7172
Catalog.defaultProps = {
7273
getUrl: '/api/v1/products'
7374
};
75+
76+
// This will make this.context available.
77+
// The store will be provided by the Provider component.
78+
Catalog.contextTypes = {
79+
store: React.PropTypes.object.isRequired
80+
};

src/store.js

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)