鍍金池/ 問(wèn)答/UI  HTML/ react項(xiàng)目,點(diǎn)擊組件就隱藏然后再點(diǎn)擊會(huì)出現(xiàn),請(qǐng)問(wèn)怎么才能實(shí)現(xiàn)?this.st

react項(xiàng)目,點(diǎn)擊組件就隱藏然后再點(diǎn)擊會(huì)出現(xiàn),請(qǐng)問(wèn)怎么才能實(shí)現(xiàn)?this.state.show無(wú)效

mport React from 'react'
import './style.css'

/**
 * 功能:默認(rèn)設(shè)置界面
 */

function getComponentCookie() {

    return import('../../utils/cookie').then(Cookie=> {

        //var timer_flag = Cookie.getCookie('timer');
        //console.log('timer_flag');
        //console.log(timer_flag)

    }).catch(error => 'An error occurred while loading the component');
}



class RightList extends React.Component {
    constructor(props) {
        super(props);

        this.state = {
            show : true
        };
    }

    handleClick() {
        var that = this;
        // 點(diǎn)擊按鈕改變樣式
        console.log('點(diǎn)擊幫助列表');
        console.log(this.state.show);
        if(this.state.show)
        {
            document.getElementById('parent').style.display = 'block';
        }
        else
        {
            document.getElementById('parent').style.display = 'none';
        }

        this.setState({show: !this.state.show});
    }

    //組件的props發(fā)生改變,在組件接收到一個(gè)新的prop時(shí)被執(zhí)行。這個(gè)方法在初始化render時(shí)不會(huì)被調(diào)用。
    componentWillReceiveProps(nextProps) {
        if(nextProps) {
            this.setState(
                {
                    text:nextProps.text
                }
            );
        }
    }

    componentDidMount() { // 初始化render之后只執(zhí)行一次

        //console.log('定時(shí)器結(jié)尾');
        document.getElementById('parent').style['fontSize'] = '85px';

    }


    render() {
        return (
            <div id='parent' onClick={this.handleClick}>
                開(kāi)啟輪播=={this.props.text}
            </div>
        )
    }
}


export default RightList;
import React from 'react'
import './style.css'

/**
 * 功能:默認(rèn)設(shè)置界面
 */


class RightList extends React.Component {
    constructor(props) {
        super(props);

        this.state = {
            show : true
        };
    }

    handleClick() {
        this.setState({show:!this.state.show});
        event.stopPropagation()
        event.preventDefault()
        // 點(diǎn)擊按鈕改變樣式
        //console.log('點(diǎn)擊幫助列表');

    }


    componentDidMount() { // 初始化render之后只執(zhí)行一次

        //console.log('定時(shí)器結(jié)尾');
        //document.getElementById('parent').style['fontSize'] = '85px';

    }


    render() {
        var show= this.state.show?"block":"none";
        var style = {
            display:show,
            fontSize: 85
        }
        return (
            <div id='parent' onClick={this.handleClick} style={style}>
                開(kāi)啟輪播=={this.props.text}
            </div>
        )
    }
}


export default RightList;

圖片描述

隱藏后就無(wú)法顯示了

import React from 'react'
import './style.css'

/**
 * 功能:默認(rèn)設(shè)置界面
 */


class RightList extends React.Component {
    constructor(props) {
        super(props);

        this.state = {
            show : true
        };
    }

    handleClick() {
        this.setState({show:!this.state.show});
        event.stopPropagation()
        event.preventDefault()

        
        // 點(diǎn)擊按鈕改變樣式
        //console.log('點(diǎn)擊幫助列表');
        if(this.state.show)
        {
            document.getElementById('parent').style.display = 'block';
        }
        else
        {
            document.getElementById('parent').style.display = 'none';
        }

    }


    componentDidMount() { // 初始化render之后只執(zhí)行一次

        //console.log('定時(shí)器結(jié)尾');
        document.getElementById('parent').style['fontSize'] = '85px';

    }


    render() {

        return (
            <div id='parent' onClick={this.handleClick.bind(this)} >
                開(kāi)啟輪播=={this.props.text}
            </div>
        )
    }
}


export default RightList;
回答
編輯回答
挽歌

handleClick的this指向有問(wèn)題,要bind(this)

 return (
            <div id='parent' onClick={this.handleClick.bind(this)}>
                開(kāi)啟輪播=={this.props.text}
            </div>
        )

或者用箭頭函數(shù)

 return (
            <div id='parent' onClick={()=>{this.handleClick()}}>
                開(kāi)啟輪播=={this.props.text}
            </div>
        )
class Header extends React.Component{
    constructor(props,el){
        super(props);
        //this.shouldComponentUpdate = PureRenderMixin.shouldComponentUpdate.bind(this);
        this.state = {
            myval:'',
            show:true
        }
    }
    change(e){
        console.log(e.target);
        this.setState({myval:e.target.value})
    }
    click(aaa){
        console.log(aaa);
    }
    toggle(){
        this.setState({show:!this.state.show})
    }
    render(){
        return(
            <header className="header">
                <input type="text"  onChange={(e)=>{this.change(e)}}/>
                <h1 style={{display:this.state.show?'block':'none'}}>{this.state.myval}</h1>  
                <button onClick={this.toggle.bind(this)}>按鈕</button>                     
            </header>
        )
    }
    componentDidMount(){

    }
}
2017年3月23日 06:49
編輯回答
尋仙

style={{display: this.state.show ? "block" : "none"}}

2017年9月15日 09:57
編輯回答
替身

不要操作dom不要操作dom不要操作dom?。?!

and

為什么隱藏之后不顯示???
兄弟,你都display: none了,這dom都沒(méi)了,你點(diǎn)都點(diǎn)不到它,你還怎么觸發(fā)它上面的click事件啊

兩種方案

1.外面包一層有寬高的div,點(diǎn)擊事件放在這個(gè)div上面

    render() {
        return (
          <div
            style={{ width: '200px', height: '200px', backgroundColor: 'red' }}
            onClick={this.handleClick}
          >
            {this.state.show && (
              <div id="parent" style={{ fontSize: '85px' }}>
                開(kāi)啟
              </div>
            )}
          </div>
        )
      }

2.改變透明度

    render() {
        let m = this.state.show ? '1' : '0'
        return (
          <div
            id="parent"
            style={{
              fontSize: '85px',
              opacity: m
            }}
            onClick={this.handleClick}
          >
            開(kāi)啟
          </div>
        )
      }
2017年3月4日 06:06
編輯回答
綰青絲

大清亡了這么多年,怎么還有人在react里操作dom

2018年5月19日 13:41
編輯回答
朕略傻

把handleClick這個(gè)函數(shù),寫(xiě)成箭頭函數(shù)試試

2018年4月5日 03:25
編輯回答
萌小萌

雖然我沒(méi)有想到比大家更好的解決辦法,但是兄弟,react里面如果操作了dom,那react存在的意義是什么?

2018年6月3日 16:20
編輯回答
凝雅
handleClick() {
    this.setState({show:!this.state.show});
}
render() {
    var show= this.state.show?"block":"none";
    var style = {
        display:show
    }
    return (
        <div id='parent' onClick={this.handleClick} style={style}>
            開(kāi)啟輪播=={this.props.text}
        </div>
    )
}
2018年1月15日 00:35