reactjs - 无法访问`action.type`,但可以访问reducer上的`action`
问题描述
我只能访问action
而不是action.type
我的减速器。我试图 console.log(action.type) 但它会给出一个未定义的值。
export const INPUT_EMAIL = 'INPUT_EMAIL'
export const INPUT_PASSWORD = 'INPUT_PASSWORD'
export function inputEmail(email) {
return {
type: INPUT_EMAIL,
email
}
}
export function inputPassword(password) {
return {
type: INPUT_PASSWORD,
password,
}
}
// import { INPUT_EMAIL, INPUT_PASSWORD } from './actions';
const initialState = {
email: '',
password: '',
}
function loginReducers(state = initialState, action: Object) {
console.log('zzZaction', action); <-- just found out it's existing
if(typeof state === 'undefined'){
return initialState
}
// switch (action.type) {
// case INPUT_EMAIL:
// return Object.assign({},state, {
// email: action.email
// })
// case INPUT_PASSWORD:
// return Object.assign({}, state, {
// password: action.password,
// })
// default:
// return state
// }
return state
}
export default loginReducers;
//MAINPAGE
import React from 'react';
import { connect } from 'react-redux';
import './Dashboard.css';
import { inputEmail, inputPassword } from './redux/actions';
import Logo from './assets/login-logo.jpg';
class LoginForm extends React.Component {
constructor(props) {
super(props);
this.state = {
email: '',
password: '',
};
this.store = this.props.store;
this.handleEmailChange = this.handleEmailChange.bind(this);
this.handlePasswordChange = this.handlePasswordChange.bind(this);
// this.handleSubmit = this.handleSubmit.bind(this);
}
handleEmailChange(event) {
this.setState({ email: event.target.value});
this.props.inputEmail(this.state.email);
}
handlePasswordChange(event) {
this.setState({ password: event.target.value });
this.props.inputPassword(this.state.password);
}
render() {
return (
<div className="Dashboard">
<div className="Dashboard-header">
<img className="Dashboard-logo" src={Logo} alt={'login-logo'} />
<h1 className="">Login Page</h1>
</div>
<div className="Dashboard-content">
<form className="Dashboard-loginContainer" onSubmit={this.handleSubmit}>
<label>
Email:
<input type="email" value={this.state.email} name="email" onChange={this.handleEmailChange} />
</label>
<label>
Password:
<input type="password" value={this.state.password} onChange={this.handlePasswordChange} />
</label>
<input type="submit" value="Log-In" />
</form>
</div>
</div>
);
}
}
// const mapStateToProps = (state) => {
// return {
// email: state.email,
// password: state.password,
// }
// }
const mapDispatchToProps = dispatch => {
return {
inputEmail: (email: string) => dispatch(inputEmail(email)),
inputPassword: (password: string) => dispatch(inputPassword(password)),
}
};
export default connect(null, mapDispatchToProps)(LoginForm);
解决方案
问题在这里:
store.subscribe((loginReducers));
您正在执行减速器功能,而不是打印出减速器状态。Subscribe
每次您的商店发生变化时都会触发。该操作实际上是第一次执行,但由于您在内部调用 loginReducers subscribe
,它运行 reducer-function 并返回错误"cannot get type of undefined."
您收到此错误是因为您尝试在没有将任何操作传递到减速器的情况下调用此函数。订阅执行得如此之快,以至于看起来您的初始操作没有执行。
这解释了为什么您能够在第一个中打印操作,console.log()
但从第二个中获取未定义。第二个console.log()
来自订阅尝试loginReducers
在没有传递任何操作时执行,返回未定义。
你想要的是这样的:
store.subscribe(() => console.log(store.getState()));
这是您更新的沙箱供参考:https ://stackblitz.com/edit/react-zjywof
现在你不应该有任何访问动作类型的错误。
推荐阅读
- excel - 将 VBA 代码限制为工作表中特定范围的单元格
- python - 从 Flask 中的 JSON 数据提供动态 matplotlib 图像
- vue.js - 如何在现有的 vue-cli 项目中创建新的 vue-cli 项目?
- python - python - 如何将一列转换为多行以获取python中的列值?
- ruby-on-rails - 在 Batch_Action 表单块中无法访问 current_user
- reactjs - 滚动反应原生后如何检查元素是否可见?(跟踪展示次数和点击次数)
- java - 构造函数 RemoteWebDriver(URL, DesiredCapabilities) 未定义
- c# - TransactionScope 如何与多线程一起工作
- flutter - 当我在 TAP 上轻触另一个图像时,想要停止上一个点击的音频吗?
- excel - Python用于比较excel列元素并在单独的列中打印匹配的元素