简体   繁体   English

在按钮上切换图标点击反应

[英]switch icons on button click in react

I am using react-full-screen library.我正在使用反应全屏库。

Link to code sandbox 代码沙箱链接

I have a navbar, where I have placed the JSX for the button with icons.我有一个导航栏,我在其中放置了带有图标的按钮的 JSX。

class AdminNavbar extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      isFfull: false
    };
  }

  render() {
    return (
      <Navbar className="navbar" expand="lg">
        <Container fluid>
          <div className="navbar-wrapper">
            <div className="navbar-minimize d-inline">
              <button className="btn-fullscreen" onClick={this.props.goFull}>
                <i className="fa fa-expand-arrows-alt"></i>
                <i className="fa compress-arrows-alt"></i>
              </button>
            </div>
          </div>
        </Container>
      </Navbar>
    );
  }
}

And then in my another Admin Component , I am using it as props and performing the onClick()然后在我的另一个Admin Component ,我将它用作道具并执行onClick()

class Admin extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      isFull: false
    };
  }

  goFull = () => {
    if (document.body.classList.contains("btn-fullscreen")) {
      this.setState({ isFull: true });
    } else {
      this.setState({ isFull: false });
    }
    document.body.classList.toggle("btn-fullscreen");
  };
  render() {
    return (
      <Fullscreen
        enabled={this.state.isFull}
        onChange={(isFull) => this.setState({ isFull })}
      >
        <div className="wrapper">
          <div className="main-panel">
            <AdminNavbar {...this.props} goFull={this.goFull} />
          </div>
        </div>
      </Fullscreen>
    );
  }
} 

Problem: the icons are not changing on click of the button.问题:单击按钮时图标没有改变。 I also tried using the active class.我也尝试使用active类。 but no luck.但没有运气。

You don't have to check the classList on body.您不必检查 body 上的 classList。 The icon toggle can be achieved by state change.Please have a look at the code.图标切换可以通过状态改变来实现。请看代码。

import React from "react";

import AdminNavbar from "./AdminNavbar";

import Fullscreen from "react-full-screen";

class Admin extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      isFull: false
    };
  }

  goFull = () => {
    this.setState({ isFull: !this.state.isFull });
  };
  render() {
    return (
      <Fullscreen
        enabled={this.state.isFull}
        onChange={(isFull) => this.setState({ isFull })}
      >
        <div className="wrapper">
          <div className="main-panel">
            <AdminNavbar
              {...this.props}
              isFull={this.state.isFull}
              goFull={this.goFull}
            />
          </div>
        </div>
      </Fullscreen>
    );
  }
}

export default Admin;

The admin Navbar code管理导航栏代码

import React from "react";

//  reactstrap components
import { Navbar, Container } from "reactstrap";

class AdminNavbar extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      isFfull: false
    };
  }

  render() {
    return (
      <Navbar className="navbar" expand="lg">
        <Container fluid>
          <div className="navbar-wrapper">
            <div className="navbar-minimize d-inline">
              <button className="btn-fullscreen" onClick={this.props.goFull}>
                {!this.props.isFull ? (
                  <i className="fa fa-expand-arrows-alt"></i>
                ) : (
                  <i className="fa compress-arrows-alt"></i>
                )}
              </button>
            </div>
          </div>
        </Container>
      </Navbar>
    );
  }
}

export default AdminNavbar;

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM