-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.js
51 lines (41 loc) · 1.28 KB
/
main.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
import React from 'react';
import ReactDOM from 'react-dom';
import { createStore, applyMiddleware, combineReducers } from 'redux'
import { connect } from 'react-redux';
import thunk from 'redux-thunk';
import { reducer as api, readEndpoint } from 'redux-json-api';
import { setEndpointHost, setEndpointPath, setAccessToken, setHeaders } from 'redux-json-api';
const reducer = combineReducers({
api
});
const store = createStore(reducer, applyMiddleware(thunk))
store.dispatch(setEndpointHost('http://localhost:8080'));
store.dispatch(setEndpointPath('/api/v1'))
class PostList extends React.Component {
componentWillMount() {
store.dispatch(readEndpoint('posts.json'));
}
render() {
return (
<div>
{this.props.posts.data.map(post => (
<h1 key={post.id} >{post.attributes.title}</h1>
))}
</div>
);
}
};
const mapStateToProps = (state) => {
console.log(state) // Check the console to see how the state object changes as we read the API
const posts = state.api.posts || { data: [] };
return {
posts
}
};
const ApiResults = connect(
mapStateToProps
)(PostList)
ReactDOM.render(
<ApiResults store={store} />,
document.getElementById('posts')
);