2017-09-26 61 views
-1

我還是當它涉及到的反應,我想創建一個隱藏的菜單,當你點擊一個按鈕,滑了一個初學者,這是我的工作:陣營 - 隱藏菜單的onclick

import React from "react"; 
import {render} from "react-dom"; 
import styles from "./MenuCocktails.css"; 
import {BottomMenu} from "./BottomMenu"; 

export class BottomMenuButton extends React.Component{ 
constructor(){ 
     super(); 
     this.state ={ 
     shown: false 
    }; 

    } 
toggleMenu(){ 
    this.setState({shown: !this.state.shown}); 
} 
render(){ 
    return(
     <div><button onClick={this.toggleMenu.bind(this)} >My Cart</button> 

      if(this.state.shown == true) { 
       <BottomMenu/> 
      } 
      else{ 
      <p>Nothing to see</p> 
      } 

     </div> 
    ); 
    } 
    } 

任何幫助是極大的讚賞

回答

2

你需要在構造函數中綁定toggleMenu

constructor(){ 
     super(); 
     this.state = { 
     shown: false 
     }; 
     this.toggleMenu = this.toggleMenu.bind(this); 
    } 

可以有條件的邏輯改變你到一個班輪:

{this.state.shown ? <BottomMenu /> : <p>Nothing to see</p>} 

你的問題是什麼?什麼不起作用?你會得到什麼錯誤?

+0

https://imgur.com/gallery/qgA4u – Ibo

+0

正如你可以在圖片中看到,我想要做的就是創建一個按鈕,隱藏和顯示內容,但它只是加載的一切瞬間之間切換,也有任何的arent錯誤 – Ibo

+0

嘿謝謝你,我不得不改變你的建議如果陳述,現在它的作品,我只需要了解如何使它現在進入一個適當的菜單,再次感謝哥們:) – Ibo