0

index.js

import store from "./store";
import { createStore } from 'redux';
import reducers from './reducers';
import { Provider } from 'react-redux';


console.log('init state', store.getState());

ReactDOM.render(    
    <Provider store={ store }>
        <App />
    </Provider>, 
    document.getElementById('root')
);

registerServiceWorker();

action.js

import * as types from "./types";
import axios from 'axios';
import { incrementProgress, decrementProgress } from "./progress";


const userLogin = username => ({
  type: types.AUTH_LOGIN,
  username,
});

const userLogout = () => ({
  type: types.AUTH_LOGOUT,
});

const fakeLoginRequest = (username, password) => {
    alert(username, password)
}

export const doLogin = username => {
    alert(username)
};

export const doLogout = () => dispatch => {
  dispatch(userLogout());
};

LoginForm.jsx - Dumb Component

class LoginForm extends React.Component {

    constructor(props) {
        super(props);
        this.state = {
            username: '',
            password: '',
        };

        this.handleClick = this.handleClick.bind(this);
    }

    fakeLogin = e => {
        const { username } = this.state;
        e.preventDefault();
        if (!username) {
          return alert("Provide a username.");
        }
        this.props.doLogin(username);
        this.setState({ username: "" });
      };

    render() {

        return (
            <div>
            <MuiThemeProvider>
              <div>
               <TextField
                 hintText="Enter your Username"
                 floatingLabelText="Username"
                 onChange = {(event,newValue) => this.setState({username:newValue})}
                 />
               <br/>
                 <TextField
                   type="password"
                   hintText="Enter your Password"
                   floatingLabelText="Password"
                   onChange = {(event,newValue) => this.setState({password:newValue})}
                   />
                 <br/>
                 <RaisedButton label="Submit" primary={true} style={style} onClick={this.fakeLogin}/>
             </div>
             </MuiThemeProvider>
          </div>
    );
}
}   

export default LoginForm;

LoginPage.jsx - Wise Component

const LoginPage = ({ auth, doLogin, doLogout }) => (

    <div>

      <NavBar/>
        <div className="row">
            <div style={{display: 'flex', justifyContent: 'center'}} className="col-md-4 col-md-offset-4">
                <LoginForm doLogin={doLogin} doLogout={doLogout} auth={auth} />
            </div>
        </div>
      </div>

);
const mapStateToProps = state => ({
  auth: state.auth,
});

export default connect(mapStateToProps, { doLogin, doLogout })(LoginPage);

I am totally new to react-redux and I am trying to create a flow that recognizes the current login status of the user. What I am trying to with the codes above is that I wanted to make sure that the username and password pass correctly and alerts them.

However, I get the error

Error: Actions must be plain objects. Use custom middleware for async actions. React-redux error.

On this.props.doLogin(username); of the LoginForm.jsx. It seems like it's also related to the onClick of the button. I don't understand because the function that I am calling is not async.

What am I doing wrongly?

1 Answer 1

2

Like the error says, your action creators have to return an object.

export const doLogin = username => {
    console.log('READ THE DOCS. IT IS VERY HELPFUL TO READ THE DOCS.');
    console.log(username);
    return {
        type: 'YOUR_ACTION_TYPE',
        payload: { whatever: 'you', want: 'it', to: 'be' }
    };
};
Sign up to request clarification or add additional context in comments.

2 Comments

what exactly does payload refer to?
It's just data. It doesn't need to be called payload. It's just a JS object. You can do whatever you like with it. The world is your oyster. Dan Abramov wrote some great tutorials on the Redux site (right next to the docs) that would be worth some of your time. redux.js.org/basics

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.