您好,我正在React中创建一个登录表单,要使用软件包的Checkbel来检查登录参数,但是当我运行代码时,会引发以下异常: ./ src / App.js尝试导入错误:“ ControlLabel”没有从“ react-bootstrap”中导出。如何解决?
反应代码:
import React, { Component } from "react";
import { Form,Button, FormGroup, FormControl, ControlLabel } from "react-bootstrap";
import "./Login.css";
import Bootstrap from "react-bootstrap";
export default class Login extends Component {
constructor(props) {
super(props);
this.state = {
email: "",
password: ""
};
}
validateForm() {
return this.state.email.length > 0 && this.state.password.length > 0;
}
handleChange = event => {
this.setState({
[event.target.id]: event.target.value
});
}
handleSubmit = event => {
event.preventDefault();
}
render() {
return (
<div className="Login">
<form onSubmit={this.handleSubmit}>
<FormGroup controlId="email" bsSize="large">
<ControlLabel>Email</ControlLabel>
<FormControl
autoFocus
type="email"
value={this.state.email}
onChange={this.handleChange}
/>
</FormGroup>
<FormGroup controlId="password" bsSize="large">
<ControlLabel>Password</ControlLabel>
<FormControl
value={this.state.password}
onChange={this.handleChange}
type="password"
/>
</FormGroup>
<Button
block
bsSize="large"
disabled={!this.validateForm()}
type="submit"
>
Login
</Button>
</form>
</div>
);
}
}
答案 0 :(得分:10)
<ControlLabel>
来自旧版本的 react-bootstrap。当前版本 ("react-bootstrap": "^1.4.3") 使用 <FormLabel>
或 <Form.Label>
答案 1 :(得分:7)
此"react-bootstrap": "^1.0.0-beta.16",
不包含ControlLabel,因此
使用
npm uninstall react-bootstrap
然后使用此"react-bootstrap": "^0.32.4"
版本
npm install react-bootstrap@0.32.4 --save
肯定会解决此问题
答案 2 :(得分:3)
根据此网址https://react-bootstrap.github.io/components/buttons/#button-props和您的版本,您应该像这样修改代码。
import React, { Component } from "react";
import Form from 'react-bootstrap/Form'
import Button from 'react-bootstrap/Button'
import Bootstrap from "react-bootstrap";
export default class Login extends Component {
constructor(props) {
super(props);
this.state = {
email: "",
password: ""
};
}
validateForm() {
return this.state.email.length > 0 && this.state.password.length > 0;
}
handleChange = event => {
this.setState({
[event.target.id]: event.target.value
});
}
handleSubmit = event => {
event.preventDefault();
}
render() {
return (
<div className="Login">
<Form onSubmit={this.handleSubmit}>
<Form.Group controlId="email" bsSize="large">
<Form.Control
autoFocus
type="email"
value={this.state.email}
onChange={this.handleChange}
/>
</Form.Group>
<Form.Group controlId="password" bsSize="large">
<Form.Control
value={this.state.password}
onChange={this.handleChange}
type="password"
/>
</Form.Group>
<Button
block
bsSize="large"
disabled={!this.validateForm()}
type="submit"
>
Login
</Button>
</Form>
</div>
);
}
}
答案 3 :(得分:0)
尝试使用FormLabel代替ControlLabel。