0

I have to change(add) className on the first div, after click on "icon-caret-down". 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});
    }
}

2
  • 1
    <div classNames= -> <div className= will already help. Commented Sep 28, 2017 at 8:37
  • I suppose you are using the classnames package and hence you need var btnClass = classnames({ 'nav-conteiner': true, 'nav-conteiner-mob': this.state.isPressed }); with a lower case n in classnames and the correction that @JulienD suggested Commented Sep 28, 2017 at 8:38

2 Answers 2

1

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.

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 });
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

I tried it doesn't work. I started use constructor. thank you. It is empty screen now
Hmmm, what do you mean? Why screen is empty? Share error please. I have already tested my snippet of code, and i didn't get any trouble with rendering or anything else.
It is a problem. It didn't give any problem.
10% building modules 1/1 modules 0 active Project is running at localhost:8080 webpack output is served from / Content not from webpack is served from ./app 404s will fallback to /
I got this and empty screen(before I added State everything was good)
0

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});
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.