Toggle Class in React

后端 未结 5 898
误落风尘
误落风尘 2020-12-13 18:27

I\'m using react for a project where I have a menu button.

         


        
相关标签:
5条回答
  • 2020-12-13 18:51

    Ori Drori's comment is correct, you aren't doing this the "React Way". In React, you should ideally not be changing classes and event handlers using the DOM. Do it in the render() method of your React components; in this case that would be the sideNav and your Header. A rough example of how this would be done in your code is as follows.

    HEADER

    class Header extends React.Component {
    constructor(props){
        super(props);
    }
    
    render() {
        return (
            <div className="header">
                <i className="border hide-on-small-and-down"></i>
                <div className="container">
                    <a ref="btn" href="#" className="btn-menu show-on-small"
                    onClick=this.showNav><i></i></a>
                    <Menu className="menu hide-on-small-and-down"/>
                    <Sidenav ref="sideNav"/>
                </div>
            </div>
        )
    }
    
    showNav() {
      this.refs.sideNav.show();
    }
    }
    

    SIDENAV

     class SideNav extends React.Component {
       constructor(props) {
         super(props);
         this.state = {
           open: false
         }
       }
    
       render() {
         if (this.state.open) {
           return ( 
             <div className = "sideNav">
                This is a sidenav 
             </div>
           )
         } else {
           return null;
         }
    
       }
    
       show() {
         this.setState({
           open: true
         })
       }
     }
    

    You can see here that we are not toggling classes but using the state of the components to render the SideNav. This way, or similar is the whole premise of using react. If you are using bootstrap, there is a library which integrates bootstrap elements with the react way of doing things, allowing you to use the same elements but set state on them instead of directly manipulating the DOM. It can be found here - https://react-bootstrap.github.io/

    Hope this helps, and enjoy using React!

    0 讨论(0)
  • 2020-12-13 18:54

    For anybody reading this in 2019, after React 16.8 was released, take a look at the React Hooks. It really simplifies handling states in components. The docs are very well written with an example of exactly what you need.

    0 讨论(0)
  • 2020-12-13 18:59

    Toggle function in react

    At first you should create constructor like this

    constructor(props) {
            super(props);
            this.state = {
                close: true,
            };
        }
    

    Then create a function like this

    yourFunction = () => {
            this.setState({
                close: !this.state.close,
            });
        };
    

    then use this like

    render() {
            const {close} = this.state;
            return (
    
                <Fragment>
    
                     <div onClick={() => this.yourFunction()}></div>
    
                     <div className={close ? "isYourDefaultClass" : "isYourOnChangeClass"}></div>
    
                </Fragment>
            )
        }
    }
    

    Please give better solutions

    0 讨论(0)
  • 2020-12-13 19:01

    refs is not a DOM element. In order to find a DOM element, you need to use findDOMNode menthod first.

    Do, this

    var node = ReactDOM.findDOMNode(this.refs.btn);
    node.classList.toggle('btn-menu-open');
    

    alternatively, you can use like this (almost actual code)

    this.state.styleCondition = false;
    
    
    <a ref="btn" href="#" className={styleCondition ? "btn-menu show-on-small" : ""}><i></i></a>
    

    you can then change styleCondition based on your state change conditions.

    0 讨论(0)
  • 2020-12-13 19:05

    You have to use the component's State to update component parameters such as Class Name if you want React to render your DOM correctly and efficiently.

    UPDATE: I updated the example to toggle the Sidemenu on a button click. This is not necessary, but you can see how it would work. You might need to use "this.state" vs. "this.props" as I have shown. I'm used to working with Redux components.

    constructor(props){
        super(props);
    }
    
    getInitialState(){
      return {"showHideSidenav":"hidden"};
    }
    
    render() {
        return (
            <div className="header">
                <i className="border hide-on-small-and-down"></i>
                <div className="container">
                    <a ref="btn" onClick={this.toggleSidenav.bind(this)} href="#" className="btn-menu show-on-small"><i></i></a>
                    <Menu className="menu hide-on-small-and-down"/>
                    <Sidenav className={this.props.showHideSidenav}/>
                </div>
            </div>
        )
    }
    
    toggleSidenav() {
        var css = (this.props.showHideSidenav === "hidden") ? "show" : "hidden";
        this.setState({"showHideSidenav":css});
    }
    

    Now, when you toggle the state, the component will update and change the class name of the sidenav component. You can use CSS to show/hide the sidenav using the class names.

    .hidden {
       display:none;
    }
    .show{
       display:block;
    }
    
    0 讨论(0)
提交回复
热议问题