Advertisement
NLinker

Simple react+redux component

Dec 2nd, 2017
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React from 'react'
  2. import PropTypes from 'prop-types'
  3. import {connect} from "react-redux";
  4. import {login, logout} from '../actions/index'
  5.  
  6. const SimpleA0 = ({loggedIn, onClick}) => {
  7.   return (
  8.     <div>
  9.       <h4>Simple Admin component</h4>
  10.       {
  11.         loggedIn ?
  12.           <p>Hey, you are logged in</p> :
  13.           <p>You are not logged in</p>
  14.       }
  15.       <button onClick={() => onClick(loggedIn)}>{
  16.         loggedIn? 'Logout': 'Login'
  17.       }</button>
  18.     </div>
  19.   )
  20. }
  21.  
  22. SimpleA0.propTypes = {
  23.   loggedIn: PropTypes.bool.isRequired,
  24.   onClick: PropTypes.func.isRequired
  25. }
  26.  
  27. const SimpleA = connect(
  28.   state => ({
  29.     loggedIn: state.user.loggedIn
  30.   }),
  31.   dispatch => {
  32.     return ({
  33.       onClick: loggedIn => {
  34.         console.log(loggedIn)
  35.         dispatch(loggedIn? logout.request(): login.request())
  36.       }
  37.     })
  38.   }
  39. )(SimpleA0)
  40.  
  41. export default SimpleA
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement