-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.js
67 lines (57 loc) · 1.78 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import React, { Component, PropTypes } from 'react';
import ReactDOM from 'react-dom';
import { connect, Provider } from 'react-redux';
import { store } from './store';
import { getUserDetails } from './action';
class UserProfile extends Component {
constructor() {
super();
this.handleUserDetail = this.handleUserDetail.bind(this);
}
componentDidMount() {
this.props.getUserDetails('row123');
}
handleUserDetail(event) {
event.preventDefault();
if (this.username !== null) {
this.props.getUserDetails(this.username.value);
this.username.value = '';
}
}
render() {
const { user } = this.props;
return (
<div>
{user ? <div>
<input
type="text"
ref={(ref) => this.username = ref}
/>
<button onClick={this.handleUserDetail}>Search</button>
<div>
<h1> User Profile </h1>
<img src={user.avatar_url}/>
<p><a href={user.html_url} target="_blank">{user.login}</a></p>
</div>
</div> : '...loading'}
</div>
)
}
}
UserProfile.propTypes = {
user: PropTypes.object.isRequired
};
// Map the store's state to component's props.
const mapStateToProps = (state) => ({ user: state });
// Wrap action creator with dispatch method. This way getUserDetails is passed in as props.
const mapDispatchToProps = (dispatch) => ({ getUserDetails: (username) => dispatch(getUserDetails(username)) });
// React-redux connect function connects our React component to redux store
const UserProfilePage = connect(mapStateToProps, mapDispatchToProps)(UserProfile);
// Mount our component to the DOM
const element = document.getElementById('root');
ReactDOM.render(
<Provider store={store}>
<UserProfilePage />
</Provider>,
element, 0
);