gpt4 book ai didi

javascript - 错误 : Actions must be plain objects. 使用自定义中间件进行异步操作。 React-redux 错误

转载 作者:行者123 更新时间:2023-12-01 01:56:15 25 4
gpt4 key购买 nike

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

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

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完全陌生,我正在尝试创建一个识别用户当前登录状态的流程。我尝试使用上面的代码是为了确保用户名密码正确通过并提醒他们。

但是,我收到错误

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

LoginForm.jsxthis.props.doLogin(username); 上。看起来它也与按钮的onClick有关。我不明白,因为我调用的函数不是异步的。

我做错了什么?

最佳答案

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

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

关于javascript - 错误 : Actions must be plain objects. 使用自定义中间件进行异步操作。 React-redux 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51014677/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com