【发布时间】:2018-02-14 00:19:41
【问题描述】:
在输入中更改用户名和密码后,我的状态属性变为“未定义”。我就是想不通:由于某种原因 this.setState 似乎不起作用。
这是代码:
import * as React from 'react';
import { Link, RouteComponentProps } from 'react-router-dom';
import { connect } from 'react-redux';
import { ApplicationState } from '../store';
import * as LogonStore from '../store/LogonStore';
type LogonProps =
LogonStore.LogonState
& typeof LogonStore.actionCreators
& RouteComponentProps<{}>;
class Logon extends React.Component<any, any> {
public constructor(props) {
super(props);
this.state = { un: '', pw: '' };
this.handleChange = this.handleChange.bind(this);
}
handleChange(event) {
console.log(event.target.value); // works fine
this.setState({ un: event.target.un, pw: event.target.pw });
console.log(this.state); //first un: '', pw: '' then un: undefined, pw: undefined
}
public render() {
return <div >
<h1>Logon</h1>
<label>username</label><input value={this.state.un} onChange={this.handleChange} />
<label>password</label><input value={this.state.pw} onChange={this.handleChange} />
<br />
</div>;
}
}
export default connect(
(state: ApplicationState) => state.logon, // Selects which state properties are merged into the component's props
LogonStore.actionCreators // Selects which action creators are merged into the component's props
)(Logon) as typeof Logon;
我在这里遗漏了一些基本的东西......
谢谢!
【问题讨论】:
标签: reactjs