Advertisement
Guest User

Untitled

a guest
Apr 16th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. class LoginComponent extends Component {
  2. constructor(props) {
  3. super(props);
  4. //... local state stuff for render()
  5. }
  6.  
  7. login() {
  8. let self = this;
  9. axios.post('/user/login', {
  10. username: this.state.username,
  11. password: this.state.password
  12. }).then(function (response) {
  13. self.setState({errorMessage: ''});
  14. self.props.setLoggedIn(true); //Here is the action that should be dispatching to change the variable.
  15. self.props.history.push('/');
  16. }).catch(function (error) {
  17. console.log(error);
  18. });
  19. }
  20.  
  21. render() {
  22. return (
  23. //A simple login form with username/password.
  24. );
  25. }
  26. }
  27.  
  28. function mapStateToProps(state) {
  29. const { loggedIn } = state;
  30. return { loggedIn };
  31. }
  32.  
  33. function mapDispatchToProps(dispatch) {
  34. return bindActionCreators({ setLoggedIn }, dispatch)
  35. }
  36.  
  37. export default connect(mapStateToProps, mapDispatchToProps)(LoginComponent);
  38.  
  39. export function setLoggedIn(loggedIn) {
  40. const action = {
  41. type: 'SET_LOGGED_IN',
  42. loggedIn
  43. };
  44. return action;
  45. }
  46.  
  47. export default ((state = [], action) => {
  48. switch (action.type) {
  49. case 'SET_LOGGED_IN':
  50. let { loggedIn } = action;
  51. return loggedIn;
  52. default:
  53. return state;
  54. }
  55. });
  56.  
  57. class NavBarComponent extends Component {
  58. test() {
  59. console.log(this.props);
  60. }
  61.  
  62. render() {
  63. console.log(this.props);
  64. return (
  65. <div><button className="btn btn-default btn-header" onClick={() => {this.test()}}>Check</button></div>
  66. );
  67. }
  68. }
  69.  
  70. function mapStateToProps(state) {
  71. const { loggedIn } = state;
  72. return { loggedIn };
  73. }
  74.  
  75. export default connect(mapStateToProps, null)(NavBarComponent);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement