在 React Js 中隐藏 Material Ui 进度条 - Reactjs
Hide Materialize ProgressBar in Reactjs - Reactjs
我是 Reactjs 的新手,我想隐藏 materialize progressBar,但是抛出了一个错误。所以我想知道我使用的方法是否不正确。是否有另一种隐藏此组件的方法。以下是错误的快照:
Error message
如果我尝试使用 getElementById
我会收到以下错误,那么我应该使用什么:
error message 2
这是我正在使用的代码:
componentDidMount() {
console.log('Parent did mount.');
document.getElementById('text_message').style.visibility = "hidden";
document.getElementsByName('progress_Bar').style.display ="none";
}
render() {
return (
<div>
<Card className="card-effects right">
<ProgressBar id="progress_Bar" name="progress_Bar"/>
<form className="card-form-signup" onSubmit={this.handleSubmit}>
<Row>
<label className="signup-header"><b>Signup to Authors Haven</b></label>
</Row>
<Row>
<Input s={12} placeholder="Username" name="username" value={this.state.username} onChange={this.handleChange} validate>
<Icon className="icon-styles">account_box</Icon></Input>
</Row>
<Row>
<Input s={12} type='email' name="email" value={this.state.email} onChange={this.handleChange} placeholder="Email" validate><Icon className="green darken-4">email</Icon></Input>
</Row>
<Row>
<Input s={12} type='password' name="password" placeholder="Password" value={this.state.password} onChange={this.handleChange} validate>
<Icon className="icon-styles">vpn_key</Icon></Input>
</Row>
<Row>
<Input s={12} type='password' name="confirm_password" placeholder="Confirm password" value={this.state.confirm_password} onChange={this.handleChange} validate>
<Icon className="icon-styles">vpn_key</Icon></Input>
</Row>
<Row>
<label >Already have an account ? </label>
</Row>
<Row>
<Button className='button-effects' type="submit" value="Submit" > Sign up </Button>
</Row>
<Row>
<label className="text-message" id="text_message" name="text_message"><i>text message</i></label>
</Row>
</form>
</Card>
</div>
);
}
React 方法是将状态设置为可见,并仅在状态为真时才呈现栏。当组件挂载时将状态设置为 false。
使用 React 时应尽量避免在渲染后操作 DOM 元素。
constructor(props) {
super(props);
this.state = {
visible: true
}
}
componentDidMount() {
console.log('Parent did mount.');
document.getElementById('text_message').style.visibility = "hidden";
this.setState({visible: false});
}
render() {
const isVisible = this.state.visible;
return (
<div>
<Card className="card-effects right">
{isVisible && <ProgressBar id="progress_Bar" name="progress_Bar"/>}
<form className="card-form-signup" onSubmit={this.handleSubmit}>
<Row>
<label className="signup-header"><b>Signup to Authors Haven</b></label>
</Row>
<Row>
<Input s={12} placeholder="Username" name="username" value={this.state.username} onChange={this.handleChange} validate>
<Icon className="icon-styles">account_box</Icon></Input>
</Row>
<Row>
<Input s={12} type='email' name="email" value={this.state.email} onChange={this.handleChange} placeholder="Email" validate><Icon className="green darken-4">email</Icon></Input>
</Row>
<Row>
<Input s={12} type='password' name="password" placeholder="Password" value={this.state.password} onChange={this.handleChange} validate>
<Icon className="icon-styles">vpn_key</Icon></Input>
</Row>
<Row>
<Input s={12} type='password' name="confirm_password" placeholder="Confirm password" value={this.state.confirm_password} onChange={this.handleChange} validate>
<Icon className="icon-styles">vpn_key</Icon></Input>
</Row>
<Row>
<label >Already have an account ? </label>
</Row>
<Row>
<Button className='button-effects' type="submit" value="Submit" > Sign up </Button>
</Row>
<Row>
<label className="text-message" id="text_message" name="text_message"><i>text message</i></label>
</Row>
</form>
</Card>
</div>
);
}
我是 Reactjs 的新手,我想隐藏 materialize progressBar,但是抛出了一个错误。所以我想知道我使用的方法是否不正确。是否有另一种隐藏此组件的方法。以下是错误的快照:
Error message
如果我尝试使用 getElementById
我会收到以下错误,那么我应该使用什么:
error message 2
这是我正在使用的代码:
componentDidMount() {
console.log('Parent did mount.');
document.getElementById('text_message').style.visibility = "hidden";
document.getElementsByName('progress_Bar').style.display ="none";
}
render() {
return (
<div>
<Card className="card-effects right">
<ProgressBar id="progress_Bar" name="progress_Bar"/>
<form className="card-form-signup" onSubmit={this.handleSubmit}>
<Row>
<label className="signup-header"><b>Signup to Authors Haven</b></label>
</Row>
<Row>
<Input s={12} placeholder="Username" name="username" value={this.state.username} onChange={this.handleChange} validate>
<Icon className="icon-styles">account_box</Icon></Input>
</Row>
<Row>
<Input s={12} type='email' name="email" value={this.state.email} onChange={this.handleChange} placeholder="Email" validate><Icon className="green darken-4">email</Icon></Input>
</Row>
<Row>
<Input s={12} type='password' name="password" placeholder="Password" value={this.state.password} onChange={this.handleChange} validate>
<Icon className="icon-styles">vpn_key</Icon></Input>
</Row>
<Row>
<Input s={12} type='password' name="confirm_password" placeholder="Confirm password" value={this.state.confirm_password} onChange={this.handleChange} validate>
<Icon className="icon-styles">vpn_key</Icon></Input>
</Row>
<Row>
<label >Already have an account ? </label>
</Row>
<Row>
<Button className='button-effects' type="submit" value="Submit" > Sign up </Button>
</Row>
<Row>
<label className="text-message" id="text_message" name="text_message"><i>text message</i></label>
</Row>
</form>
</Card>
</div>
);
}
React 方法是将状态设置为可见,并仅在状态为真时才呈现栏。当组件挂载时将状态设置为 false。
使用 React 时应尽量避免在渲染后操作 DOM 元素。
constructor(props) {
super(props);
this.state = {
visible: true
}
}
componentDidMount() {
console.log('Parent did mount.');
document.getElementById('text_message').style.visibility = "hidden";
this.setState({visible: false});
}
render() {
const isVisible = this.state.visible;
return (
<div>
<Card className="card-effects right">
{isVisible && <ProgressBar id="progress_Bar" name="progress_Bar"/>}
<form className="card-form-signup" onSubmit={this.handleSubmit}>
<Row>
<label className="signup-header"><b>Signup to Authors Haven</b></label>
</Row>
<Row>
<Input s={12} placeholder="Username" name="username" value={this.state.username} onChange={this.handleChange} validate>
<Icon className="icon-styles">account_box</Icon></Input>
</Row>
<Row>
<Input s={12} type='email' name="email" value={this.state.email} onChange={this.handleChange} placeholder="Email" validate><Icon className="green darken-4">email</Icon></Input>
</Row>
<Row>
<Input s={12} type='password' name="password" placeholder="Password" value={this.state.password} onChange={this.handleChange} validate>
<Icon className="icon-styles">vpn_key</Icon></Input>
</Row>
<Row>
<Input s={12} type='password' name="confirm_password" placeholder="Confirm password" value={this.state.confirm_password} onChange={this.handleChange} validate>
<Icon className="icon-styles">vpn_key</Icon></Input>
</Row>
<Row>
<label >Already have an account ? </label>
</Row>
<Row>
<Button className='button-effects' type="submit" value="Submit" > Sign up </Button>
</Row>
<Row>
<label className="text-message" id="text_message" name="text_message"><i>text message</i></label>
</Row>
</form>
</Card>
</div>
);
}