Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class LoginComponent extends Component {
- constructor(props) {
- super(props);
- //... local state stuff for render()
- }
- login() {
- let self = this;
- axios.post('/user/login', {
- username: this.state.username,
- password: this.state.password
- }).then(function (response) {
- self.setState({errorMessage: ''});
- self.props.setLoggedIn(true); //Here is the action that should be dispatching to change the variable.
- self.props.history.push('/');
- }).catch(function (error) {
- console.log(error);
- });
- }
- render() {
- return (
- //A simple login form with username/password.
- );
- }
- }
- function mapStateToProps(state) {
- const { loggedIn } = state;
- return { loggedIn };
- }
- function mapDispatchToProps(dispatch) {
- return bindActionCreators({ setLoggedIn }, dispatch)
- }
- export default connect(mapStateToProps, mapDispatchToProps)(LoginComponent);
- export function setLoggedIn(loggedIn) {
- const action = {
- type: 'SET_LOGGED_IN',
- loggedIn
- };
- return action;
- }
- export default ((state = [], action) => {
- switch (action.type) {
- case 'SET_LOGGED_IN':
- let { loggedIn } = action;
- return loggedIn;
- default:
- return state;
- }
- });
- class NavBarComponent extends Component {
- test() {
- console.log(this.props);
- }
- render() {
- console.log(this.props);
- return (
- <div><button className="btn btn-default btn-header" onClick={() => {this.test()}}>Check</button></div>
- );
- }
- }
- function mapStateToProps(state) {
- const { loggedIn } = state;
- return { loggedIn };
- }
- export default connect(mapStateToProps, null)(NavBarComponent);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement