【发布时间】:2019-02-03 22:23:07
【问题描述】:
我在使用 eslint 时遇到了一些问题。它要求我使用解构道具分配,但是当我更改代码时,它会中断。
有什么想法吗?
这是完整的代码
class LoginPage extends React.Component {
submit = data =>
// This is how I tried to fix it!
// {
// const { login, history } = this.props;
// login(data).then(() => history.push('/'));
// };
// This is what I have, its working but eslint is complaining.
this.props.login(data).then(() => this.props.history.push('/'));
render() {
return (
<div>
<h1>Login Page</h1>
<LoginForm submit={this.submit} />
</div>
);
}
}
LoginPage.propTypes = {
history: PropTypes.shape({
push: PropTypes.func.isRequired
}).isRequired,
login: PropTypes.func.isRequired /* eslint-disable-line */
};
export default connect(
null,
{ login }
)(LoginPage);
修改后的代码出现的错误是:
TypeError: Object(...)(...).then is not a function
LoginPage._this.submit
src/ components/pages/LoginPage.js:10
7 | class LoginPage extends React.Component {
8 | submit = data => {
9 | const { login, history } = this.props;
10 | login(data).then(() => history.push('/'));
11 | };
12 |
13 | render() {
它说问题在第 10 行。
任何帮助将不胜感激!
【问题讨论】:
标签: reactjs destructuring