我是Reactjs中的新手,我想隐藏实现 progressBar,但是有一个错误被抛出。所以我想知道现在使用的方法是不是正确的。还有其他方法隐藏这个组件吗。下面是错误的快照:
如果我尝试使用getElementById,我会在下面得到这个错误,那么我应该使用什么:
这是正在使用的代码:
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>
);
}发布于 2018-10-09 13:33:19
这种反应方式是将状态设置为可见的,只有当状态为true时才呈现bar。当组件挂载时,将状态设置为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>
);
}https://stackoverflow.com/questions/52722065
复制相似问题