当我尝试更改输入字段上的值时收到以下警告。
Can't call setState on a component that is not yet mounted. This is a no-op, but it might indicate a bug in your application. Instead, assign to
this.state
directly or define astate = {};
class property with the desired state in the Login component.
我不明白为什么会发生这种情况,但在我的任何其他 React 页面上都没有发生。
唯一的区别是这是登录页面,因此有自己的页面路由。
有人可以帮忙吗?我已将我的组件剥离回最基本的结构,看看是否有人能发现我做错了什么。
import React, { Component } from 'react';
class Login extends Component {
constructor(props) {
super(props);
this.state = {
username: ''
};
this.onChange = this.onChange.bind(this);
}
onChange(e) {
this.setState({ [e.target.name]: e.target.value });
}
render() {
const { username } = this.state;
return (
<input type="text" placeholder="Email" className="form-control" name="username" autoComplete="email" value={username} onChange={this.onChange} />
);
}
}
export default Login;
最佳答案
问题不是因为我的组件,而是因为我更改了我的 webpack 配置以支持 async await es7 功能。到以下...
"presets": [ "es2017" , "stage-0" , "react" ]
相反,我实际上需要做下一个答案并安装
npm install --save-dev regenerator-runtime
https://stackoverflow.com/a/51650116/3284707
从上面的链接中得到答案解决了我的问题。
关于javascript - react 应用程序。无法在尚未安装的组件上调用 setState,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52092595/