简体   繁体   English

单击React中的另一个元素后如何将className更改为元素

[英]How to change className to element after click on another element in React

I have to change(add) className on the first div, after click on "icon-caret-down". 单击“ icon-caret-down”后,我必须在第一个div上更改(添加)className。 My code doesn't work. 我的代码不起作用。 Can you give me some tips? 你能给我一些提示吗?

 export default class Nav extends React.Component { render() { var btnClass = classNames({ 'nav-conteiner': true, 'nav-conteiner-mob': this.state.isPressed }); return ( <div classNames={btnClass}> <span className='icon-soundcloud'></span> <h6 id="site-name">SoundCloud</h6> <span className="icon-caret-down" onClick={this.openSerch.bind(this)}></span> <ul> <li><a href='#'>Explore</a></li> <li><a href='#'>Playlist</a></li> </ul> <Search/> </div> ); } openSerch(){ console.log('hello'); this.setState({isPressed:true}); } } 

I guess the main error that you didn't announce initial state. 我猜主要的错误是您没有宣布初始状态。

The next thing, that you used wrong attribute "classNames" instead "className" to wrapper. 接下来,您对包装器使用了错误的属性“ classNames”而不是“ className”。

I corrected mistakes, check it out: 我已更正错误,请检查一下:

export default class Nav extends React.Component {

    constructor (props) {
        super(props);
        this.state = {
            isPressed: false
        }
    }

    render () {
        var btnClass = classNames({
            'nav-conteiner': true,
            'nav-conteiner-mob': this.state.isPressed
        });

        return (
            <div className={btnClass}>
                <span className='icon-soundcloud'></span>
                <h6 id="site-name">SoundCloud</h6>
                <span className="icon-caret-down" onClick={this.openSerch.bind(this)}></span>
                <ul>
                    <li><a href='#'>Explore</a></li>
                    <li><a href='#'>Playlist</a></li>
                </ul>
                <Search/>
            </div>
        );
    }

    openSerch () {
        this.setState({ isPressed: true });
    }
}

 export default class Nav extends React.Component { constructor (props) { super(props); this.state = { isPressed: false } } render() { var btnClass = classNames({ 'nav-conteiner': true, 'nav-conteiner-mob': this.state.isPressed }); return ( <div classNames={this.state.isPressed ? btnActiveClass :btnClass }> <span className='icon-soundcloud'></span> <h6 id="site-name">SoundCloud</h6> <span className="icon-caret-down" onClick={this.openSerch.bind(this)}></span> <ul> <li><a href='#'>Explore</a></li> <li><a href='#'>Playlist</a></li> </ul> <Search/> </div> ); } openSerch(){ console.log('hello'); this.setState({isPressed:true}); } } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 React 中,如何更改在迭代中创建的一个元素的类名,即另一个元素的 onclick? - In React, how do I change the classname of one element that was created in an iteration, onclick of another element? 如何在单击时更改元素的 className? (汉堡菜单) - How can I change the className of an element on click? (hamburger menu) 根据反应路由器中的路由更改元素的类名 - change classname of element based on route in react router 在函数中的元素上更改className - Change className on Element in a function 如何在反应中将className添加到变量内的元素? - How to add a className to an element inside a variable in react? 如何使用类名更改元素的可见性 - How to change visibility of element using a classname 如何在另一个元素 REACT 上更改元素 onClick 的样式? - How to change style of an element onClick at another element REACT? 如何在地图方法中单击时仅禁用一个按钮,以及如何在 React 中的计时器到期后更改 className? - How to make only one button disabled on click in map method and how to change className after the timer expires in React? 我们如何在另一个组件中单击按钮时更改反应组件中元素的 state? - How do we change the state of element in a react component on button click in another component? 如何在另一个元素中更改元素 - How to change element in another element
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM