[英]Error server Error: Actions must be plain objects. Use custom middleware for async actions
I'm trying to write a React web app, but I received an error.我正在尝试编写 React Web 应用程序,但收到错误消息。 I can't understand why it doesn't work.
我不明白为什么它不起作用。 When a user enter in my web app, I want to save his email address in a Redux store.
当用户进入我的 Web 应用程序时,我想将他的电子邮件地址保存在 Redux 商店中。
types.js类型.js
export const LOGIN = 'LOGIN';
export const DATA = 'DATA';
Here I defined the type of my actions在这里我定义了我的动作类型
searchActions.js搜索操作.js
import {LOGIN, DATA} from './types';
import axios from 'axios';
import store from '../store'
export const login = ()=> {
store.dispatch({
type: LOGIN
})
};
export const information = async(email) => {
store.dispatch({
type: DATA,
payload: email
})
}
Here wrote what my actions have to do according to the type.这里根据类型写了我的动作要做什么。
userReducer.js userReducer.js
import {LOGIN, DATA} from '../actions/types';
const initialState = {
isLogged: false,
email: '',
}
export default function(state = initialState, action){
switch(action.type){
case LOGIN:
return {
...state,
isLogged: true
};
case DATA:
return {
...state,
email: action.payload
};
default:
return state;
}
}
This is a simple form.这是一个简单的表格。 If the response from the database is positive I want to save his mail in the store.
如果数据库的响应是肯定的,我想将他的邮件保存在商店中。 If I delete the 41st line it works well.
如果我删除第 41 行,它运行良好。 SignIn.jsx
登录.jsx
import React, { Component } from 'react';
import { connect } from 'react-redux';
import {login, information} from '../actions/searchActions';
import './component.css'
import axios from 'axios';
const baseUrl = "http://localhost:3000";
class SignIn extends Component {
state = {
email:'',
password: '',
}
render() {
const handleChange = (e) => {
this.setState({[e.target.name]: e.target.value});
}
const handleSubmit = (e) => {
//aggiungere la validazione del form in una futura versione
e.preventDefault();
access();
}
const access = () => {
const url = baseUrl + '/router/sign-in';
axios.get(url, {
params: {
email: this.state.email,
password: this.state.password
}
})
.then(res=>{
if (res.data.status !== undefined) {
const data = res.data.status
console.log(JSON.stringify(data)) //users entrato
login();
this.props.history.push('/')
this.props.information(this.state.email) // HERE IS THE ERROR.
// if I delate this line it works
}
else {
alert(res.data.error)
}
})
.catch(error=>{
alert("Error server "+ error)
})
}
return (
<div className="firstly-square">
<div className="center-form">
<form action="" className="sign" onSubmit={handleSubmit}>
<div className="title-sign">
SignIn
</div>
<div className="signin-username">
<label htmlFor="email" className="signin-label">email</label>
<input type="text" name="email" className="signin-input" onChange={handleChange} value={this.state.email}/>
</div>
<div className="signin-password">
<label htmlFor="password" className="signin-label">password</label>
<input type="password" name="password" className="signin-input" onChange={handleChange}/>
</div>
<div className="signin-bottone">
<button type="submit" className="signin-button">Sumbit</button>
</div>
</form>
</div>
</div>
);
}
}
const mapStateToProps = state => ({
email : state.user.email
});
export default connect( mapStateToProps, { information })(SignIn);
Please, help me.请帮我。 Thanks
谢谢
This is my store.js这是我的 store.js
import thunk from 'redux-thunk';
import {composeWithDevTools} from 'redux-devtools-extension';
import rootReducer from './reducers'
const middleware = [thunk];
const initialState = {};
const store = createStore(rootReducer, initialState, composeWithDevTools(applyMiddleware(...middleware)));
export default store;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.