变量没有在 Reactjs 中取变量的初始值

IT技术 reactjs
2021-05-03 11:07:53

我正在用 reactjs 制作一个图像查看器。实际上我希望当用户单击旋转图标时图像旋转约 90 度。一切都进展顺利,但主要问题是当我点击任何图像并旋转它并关闭它之后,如果我打开其他它会采用以前的旋转值,但在我初始化时它必须为零。

class GalleryModal extends React.Component {
        constructor(props) {
          super(props);
          this.state = {
            rotation: 0
          };
          this.rotate = this.rotate.bind(this);
          this.fullScreen = this.fullScreen.bind(this);
        }

        render() {
          const { rotation } = this.state;
          if (this.props.isOpen === false) {
            return null;
          }

          return (
            <div className="modal-overlay" name={this.props.name}>
              <div className="modal-body" id="image_container">

                <img
                  className="center_image"
                  id="image"
                  src={this.props.src}
                  style={{ transform: `rotate(${rotation}deg)` }}
                />
                <a href="#" className="fullscreen button" onClick={this.fullScreen}>
                    <i className="fas fa-compress-arrows-alt"></i>
                </a>
                <a href="#" className="button" onClick={() => this.rotate()}>
                  <i className="fas fa-sync-alt" />
                </a>
                <a href="#" className="button" onClick={this.props.onPrev}>
                  <i className="fas fa-angle-left" />
                </a>
                <a href="#" className="button" onClick={this.props.onNext}>
                  <i className="fas fa-angle-right" />
                </a>
                <a
                  className="modal-close"
                  href="#"
                  onClick={this.props.onClick}
                >
                  <span className="fa fa-times" />
                </a>

              </div>
            </div>
          );
        }
        rotate() {
          let newRotation = this.state.rotation + 90;
          if (newRotation >= 360) {
            newRotation = -360;
          }
          this.setState({
            rotation: newRotation
          });
        }

        fullScreen() {
          let elem = document.getElementById("image_container");
          if (elem.requestFullscreen) {
            elem.requestFullscreen();
          } else if (elem.mozRequestFullScreen) {
            /* Firefox */
            elem.mozRequestFullScreen();
          } else if (elem.webkitRequestFullscreen) {
            /* Chrome, Safari & Opera */
            elem.webkitRequestFullscreen();
          } else if (elem.msRequestFullscreen) {
            /* IE/Edge */
            elem.msRequestFullscreen();
          }
        }
      }

Demo here这是我尝试这样做的目的,但是当我单击另一个图像或下一个/上一个图标时,它会采用前一个值,但rotation在我声明时必须为零rotation:0我不知道旋转错误是从哪里出现的,因为我认为我做对了。你能告诉我我做错了什么以及如何解决吗????

2个回答

问题是,当您设置状态具有 时rotation: 0,您正在修改Gallery组件的状态因此,当您打开模型时,GalleryComponent 将自己的this.state.rotation变量设置为0

相反,用于显示图像的旋转是GalleryModalComponent 中的旋转,永远不会0再次设置

解决方案可能是将this.state.rotation变量从Gallery组件传递GalleryModal组件。

这是你的小提琴固定:https : //jsfiddle.net/3hywna07/

此外,在第 61 行,而不是写:

<a href="#" className="button" onClick={() => this.props.rotate()}>

写就好了:

<a href="#" className="button" onClick={this.props.rotate}>

render()应该避免方法内部使用箭头函数,因为它会导致每次render()执行方法时都会创建函数

更新链接:- https://jsfiddle.net/c0qpk5wv/2/

马特奥做对了。您还可以让模态本身保持旋转并创建一个关闭函数,该函数将在调用提供的关闭函数之前将旋转设置为 0。Gallery 不应该对旋转感兴趣,因为在图像之间保持旋转几乎没有value。