我是React的新手,我想在點擊後隱藏按鈕。下面的兩個代碼段下面是proptypes。我是否需要隱藏提交按鈕?點擊後如何隱藏按鈕?我讀到,我可以通過州或CSS來實現。既然這是一個按鈕,它似乎會更容易使用CSS?任何幫助,將不勝感激。如何使用React隱藏按鈕
0
A
回答
2
請參閱有關如何有條件地隱藏元素此琴:
https://jsfiddle.net/69z2wepo/77987/
基本理念是,在渲染完成這一工作;
render: function() {
return (<div>
{this.state.clicked && <div>Shown or hidden?</div>}
</div>);
}
這個想法是你依賴組件狀態來決定是否應該呈現任何東西。您操縱強制重新渲染組件的狀態。
我認爲這是一個「更好」的方式比使用CSS,但CSS有它的用途。(這可以用於一個「隱藏」類有條件地添加到元)
0
/* eslint-disable jsx-a11y/img-has-alt,class-methods-use-this */
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import todoStyle from 'src/style/todo-style.scss';
import { Router, Route, hashHistory as history } from 'react-router';
import Myaccount from 'src/components/myaccount.jsx';
export default class Headermenu extends Component {
constructor(){
super();
// Initial state
this.state = { open: false };
}
toggle() {
this.setState({
open: !this.state.open
});
}
componentdidMount() {
this.menuclickevent = this.menuclickevent.bind(this);
this.collapse = this.collapse.bind(this);
this.myaccount = this.myaccount.bind(this);
this.logout = this.logout.bind(this);
}
render() {
return (
<div>
<div style={{ textAlign: 'center', marginTop: '10px' }} id="menudiv" onBlur={this.collapse}>
<button onClick={this.toggle.bind(this)} > Menu </button>
<div id="demo" className={"collapse" + (this.state.open ? ' in' : '')}>
<label className="menu_items" onClick={this.myaccount}>MyAccount</label>
<div onClick={this.logout}>
Logout
</div>
</div>
</div>
</div>
);
}
menuclickevent() {
const listmenu = document.getElementById('listmenu');
listmenu.style.display = 'block';
}
logout() {
console.log('Logout');
}
myaccount() {
history.push('/myaccount');
window.location.reload();
}
}
enter code here
相關問題
- 1. 使用.prop隱藏按鈕(隱藏:真)
- 2. 如何隱藏按鈕
- 3. 隱藏按鈕
- 4. 隱藏按鈕
- 5. 如何隱藏或禁用按鈕?
- 6. 使用VBA if語句用按鈕隱藏/取消隱藏列
- 7. 隱藏HTML5按鈕
- 8. 使用隱藏按鈕登錄腳本
- 9. 使用Javascript隱藏提交按鈕
- 10. 隱藏/顯示nicEdit使用按鈕
- 11. 使用c顯示隱藏按鈕#
- 12. 隱藏按鈕不會使用jQuery
- 13. Xcode - 如何使用委託方法隱藏取消隱藏按鈕
- 14. VBA隱藏/取消隱藏按鈕
- 15. 使用Webview滾動時隱藏浮動按鈕隱藏
- 16. 如何隱藏DataGridTemplateColumn.CellTemplate中的按鈕
- 17. 如何隱藏桌面上的按鈕
- 18. 如何隱藏溢出按鈕?
- 19. 如何顯示/隱藏按鈕?
- 20. 如何隱藏url中的按鈕值?
- 21. 如何隱藏這個「關閉」按鈕
- 22. Xcode中如何隱藏按鈕
- 23. 如何隱藏div單選按鈕
- 24. 如何在html中隱藏按鈕?
- 25. 單擊按鈕時如何隱藏SurfaceView?
- 26. radioGroup如何隱藏按鈕android
- 27. 如何隱藏點擊按鈕(WordPress的)
- 28. 如何隱藏Facebook註銷按鈕
- 29. 如何隱藏按鈕輪廓
- 30. 如何隱藏使用模板10的後退按鈕?
添加狀態'''{shouldShowButton:true}'',點擊更改狀態。 ''{shouldShowButton:false}'''然後添加class.'''className = {shouldShowButton? '':'hidden'}''' –
可能對你有幫助[show/hide codepen](https://codepen.io/ulugtoprak/pen/oWGqBp) –