私はちょうどReactを使い始め、小さなアプリで作業していますが、その間は小さなショーをしてモーダルを隠しています。私はそれをやり方が間違っている方法を知りたがっていました。これがアンチパターンの場合はどうすればいいですか?私は、このロジックを使用して行ったこれは反パターンですか?Reactjs
class App extends Component {
constructor(props) {
super(props);
this.state = {show: false};
this.showModal = this.showModal.bind(this);
}
render() {
return (
<div>
<h2 className={styles.main__title}>Helloooo!</h2>
<Modal ref='show'/>
<button onClick={this.showModal} className={styles.addtask}>➕</button>
</div>
);
}
showModal(){
this.setState({
show: true
});
this.refs.show.showModal();
}
}
モーダル成分は、それがDOM要素をフックとdocument.queryselector
を使用して修正します。これは、反応におけるドム操作を行う正しい方法ですか?私が使用している
モーダルコードはこれです:
class Modal extends Component {
constructor() {
super();
this.hideModal = this.hideModal.bind(this);
this.showModal = this.showModal.bind(this);
this.state = { modalHook: '.'+styles.container };
}
render() {
return (
<div>
<div onClick={this.hideModal} className={styles.container}>
<div className={styles.container__content}>
<div className={styles.card}>
<div className={styles.card__header}>
<h2>Add new task</h2>
</div>
<div className={styles.card__main}>
<Input type="text" placeholder="enter the task title" />
<Input type="textarea" placeholder="enter the task details" />
</div>
<div className={styles.card__actions}>
</div>
</div>
</div>
</div>
</div>
);
}
showModal(){
let container = document.querySelector(this.state.modalHook);
container.classList.add(styles.show);
}
hideModal(e){
let container = document.querySelector(this.state.modalHook);
if(e.target.classList.contains(styles.container)){
container.classList.remove(styles.show);
}
}
}
あなたのコードを投稿してください、いくつかの美しい写真ではありません。 – Bergi
この同じAppコンポーネントを2度追加しました。 –
あなたが使っていないので、小道具を手に入れる必要はありません。他のすべては実行可能なようです。何が間違っていると思いますか? – zfrisch