错误:动作必须是普通对象。使用自定义中间件进行异步操作。 React-redux错误

时间:2018-06-24 23:10:34

标签: javascript reactjs redux react-redux

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-哑组件

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-明智的组件

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);

我对react-redux完全陌生,我正在尝试创建一个可识别用户当前登录状态的流。我要使用上面的代码尝试的是,我想确保usernamepassword正确通过并发出警报。

但是,我得到了错误

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

LoginForm.jsx this.props.doLogin(username);上。似乎它也与按钮的onClick有关。我不明白,因为我正在调用的函数不是异步的。

我做错了什么?

1 个答案:

答案 0 :(得分:2)

就像错误提示一样,您的动作创建者必须返回一个对象。

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' }
    };
};