2017-02-14 5 views
1

inputがクリックされたときにoverlayページが表示されています。今度はoverlayページのユーザーclicksのどこかにあるoverlayのページを削除したいと考えています。どうやってやるの?コンポーネントを削除して反応します

私は私のoverlayがコンポーネントにQuestionOverlay

class QuestionOverlay extends Component { 
    constructor() { 
     super(); 
    } 

    closeOverLay = (e) => { 
     alert("fse"); 
    } 

    render() { 
     return (
      //Here I have implemented my overlay 
     ) 
    } 

} 

export default QuestionOverlay; 

で、どのように私は私がどこかをクリックするとオーバーレイコンポーネントを削除/閉じることができ、この

constructor(props) { 
     super(props); 

     this.state = { 
      showComponent: false, 
     }; 
     this.popup_ques = this.popup_ques.bind(this); 

    } 

    popup_ques() { 
     this.setState({ 
      showComponent: true, 
     }); 
    } 

render() { 
     return (
      <div className="ff"> 
       <div className="middle_div"> 
        <input className='post_data_input' placeholder="Ask your question here" ref="postTxt" onClick={this.popup_ques}/> 
       </div> 

       {this.state.showComponent ? <QuestionOverlay/> : null} 

      </div> 
     ); 
    } 

などのクリックでオーバーレイを表示しています私のオーバーレイ?

答えて

1

オーバレイのonClickと呼ばれるオーバーレイの親コンポーネント(オーバーレイを表示するコンポーネント)から関数を渡します。この機能は、オーバレイを隠すために親のthis.state.showComponentfalseに更新します。

constructor(props) { 
    super(props); 

    this.state = { 
     showComponent: false, 
    }; 
    this.popup_ques = this.popup_ques.bind(this); 
    this.hide_overlay = this.hide_overlay.bind(this); 

} 

popup_ques() { 
    this.setState({ 
     showComponent: true, 
    }); 
} 

hide_overlay() { 
    this.setState({ 
    showComponent: false 
    }) 
} 

render() { 
    return (
     <div className="ff"> 
      <div className="middle_div"> 
       <input className='post_data_input' placeholder="Ask your question here" ref="postTxt" onClick={this.popup_ques}/> 
      </div> 

      {this.state.showComponent && <QuestionOverlay hideOverlay={this.hide_overlay} />} 

     </div> 
    ); 
} 

オーバーレイ

class QuestionOverlay extends Component { 
    constructor() { 
    super(); 
    } 

    closeOverLay = (e) => { 
    alert("fse"); 
    } 

    render() { 
    return (
     <div onClick={this.props.hideOverlay}> 
      // Overlay content 
     </div> 
    ) 
    } 

} 

export default QuestionOverlay; 
+2

私は結合 'this.hideOverlay.bind(この)'でコンストラクタを行うには良いだろうと思います。 –

+0

@Brett DeWoody:あなたのアプローチは良いですが、それは私のためには機能しません。私は、クリックイベントが機能するかどうかを知るために警告を挿入しました。それはそうです。問題は、コンポーネントの状態をfalseに変更していないことです。 – CraZyDroiD

+0

三項とは対照的に '{this.state.showComponent && }を試してください。 ' –

関連する問題